Automatically parse the image address in the web with ASP and save it to the local server

zhaozj2021-02-16  48

Now the web page-based HTML editor is used in the news system, the article system is getting wider and wider, and a web page can maintain the original style, while the picture can be kept in this page. But during use, if the picture in the paste page is deleted, you will leave a big "X" on your own page, affecting the beautiful view. I had to save this picture before, and then upload it to the server, so it is really troublesome. Can you let the server download the image to save the link on the server and replace the link on the page? The answer is yes. To achieve this feature requires three steps: 1. Get the address of the image in the home page. There are many ways, you can use a split string or by regular matching. Practice has proved that the regular match is the most simple. The address of the analysis picture is saved in the tab. We can get all this label first. The process is as follows: set objRegexp = new regexp 'Settings Configuration Object ObjRegexp.IgnoreCase = true' ignore the case ObjRegexp.global = true 'Settings to Full-text Search ObjRegexp.pattern = "" To ensure accurate removal The picture address is divided into two-layer configuration: first find the tab inside, then remove the getImgs function behind the image address inside is the implementation of the implementation. STRS = TRIM (STR) SET MATCHES = ObjRegexp.execute (strs) Start Performing for Each Match In Matches Retstr = RetStr & GetImgs (Match.Value) 'Executes the second round of matching next

All pictures are in this SRC = "http: // picture address", so it can be used to achieve the exact picture address: function getimgs (str) getimgs = "" set objregexp1 = new regexpobjregexp1.ignorecase = trueobjregexp1. Global = trueobjregexp1.pattern = "http://. ?" "'Remove the address in the address SET MM = Objregexp1.execute (str) for each match1 in mmgetimgs = getimgs &" || "& left (match1.value, len) Match1.value) -1) 'Take the address in the back of Nextend Function

We have the address of all pictures, we can do the second step. Second, download the picture and save it on the server. This can also be divided into two steps: one is the content of the picture and the other is saved on the server. The content of the picture is implemented by the following function: function gethttppage (URL) on error resume nextdim httpset http = server.createObject ("msxml2.xmlhttp") 'Using XMLHTTP method to get the picture of the picture http.open "Get ", url, falsehttp.send () if http.readystate <> 4 THEN EXIT functionEND IFETHTTPPAGE = http.responsebodyset http = Nothingif err.number <> 0 Then Err.Clear End Function gets the picture content to save, give one The feeling is that it can be used in FSO, but it is actually not, so that the save program will be wrong because the FSO does not support streaming files, so we have to call another object: ado.strem. Specific process is as follows: function process with Hugh content saveimage (from, tofile) dim geturl, objStream, imgsgeturl = trim (from) imgs = gethttppage (geturl) 'get the picture Set objStream = Server.CreateObject ( "ADODB.Stream" ) 'Establish AdoDB.Stream objects, you must have ADO 2.5 or above ObjStream.Type = 1' Open ObjStream.OpenObjstream.Write Imgs 'in binary mode to write string contents to buffer Objstream.savetofile Server.mAppath (Tofile), 2' - Write the buffered content writes ObjStream.close () 'Close the object set objstream = Nothingend function, so as long as one loop is saved all the pictures of the addresses you have, the specific process is as follows: arrimg = split (RetStr, " || ") 'Split String, acquire the list address list allimg =" "newimg =" "for i = 1 to Ubound (arrimg) IF Arrimg (i) <>" and INSTR (Allimg, Arrimg (i)) < 1 THEN 'Look at this picture has been downloaded with FName = Baseurl & CSTR (I & MID (Arrimg (i), "))) SaveImage (arrimg (i), fname)' saves the address of the address, process See above allimg = allimg & "| & arrimg (i) 'puts up the address of the saved picture to determine the address to be replaced newimg = newimg &" | "& fname" back the local address string end ifnext Three steps is to replace the original address.

The specific process is below: Arrnew = split (newimg, "||") 'gets the original image address list arraall = split (allimg, "||")' gets the address list of the selected picture that has been saved for i = 1 To Ubound (arrnew) 'Performing a loop Replace the original address strs = Replace (STRS, Arrall (i), ArrNew (i)) NextcctV = strs tells here, this function's basic process is this, of course, it can be transformed You can achieve more features, such as adding image size limits, coupled with the limit on image downloads on the local machine, so as not to add repeated download pictures. At the same time, it should also be seen that the deficiencies of this function is to handle the static image files and cannot apply pictures generated. Everyone has any good comments and suggestions can be contacted with me: QQ: 26452218. I have DLL in my hand. You can contact me if you want.

转载请注明原文地址:https://www.9cbs.com/read-24805.html

New Post(0)