Use XMLHTTP to resolve picture addresses and save

Source: Internet
Author: User
Tags split string back trim
XML is now based on the Web page of the HTML editor in the news system, article system used more and more widely, a Web page can maintain the original style, while the picture can be maintained on this page. However, in the use of the process, if the pasted pages of the picture is deleted, you will leave a large "X" on their own page, affecting the beautiful. Previously had to save this picture, and then uploaded to the server, this is really troublesome. Can I have the server automatically download pictures saved on the server and replace the links on the page? The answer is yes.





to achieve this functionality requires three steps:


one, get the address of the picture on the original page. There are many ways to use split strings, or you can use regular matches. It is proved by practice that regular matching is the simplest. The addresses of the analyzed pictures are kept in the <img> tag. We can get all this tag first. The process is as follows:


Set objregexp = New Regexp ' Set Configuration object


objregexp.ignorecase = True ' ignores case


Objregexp.global = True ' Set to Full-text search


Objregexp.pattern = "<img.+?>" in order to ensure accurate removal of the picture address, so divided into two configurations: first find the inside of the <img> tag, And then take out the image inside the Getimgs function behind the address is to achieve the latter function.


Strs=trim (str)


set matches =objregexp.execute (STRs) ' Starts execution configuration


for each Match in matches


retstr = Retstr &getimgs (match.value) ' performs a second-round match


Next


all the pictures inside are like this src= "http://picture address", so you can get the exact picture address:


function Getimgs (str)


getimgs= ""


Set objRegExp1 = New Regexp


objregexp1.ignorecase = True


Objregexp1.global = True


Objregexp1.pattern = "http://.+?" "" Take out the address inside


set Mm=objregexp1.execute (str)


for each Match1 in mm


getimgs=getimgs& "| |" &left (Match1.value,len (Match1.value)-1) ' Put the address of the inside string up for backup


Next


End Function





gets the address of all the pictures, we can do the second step of the operation.


Two, download the picture and save it on the server. This can be divided into two steps: one is to get the content of the picture, the other is saved on the server. The content of the picture is achieved by using the following function:





function gethttppage (URL)


On Error Resume Next


Dim http


set Http=server.createobject ("Msxml2.xmlhttp") ' uses the XMLHTTP method to get the contents of the picture


Http.open "Get", Url,false


http.send ()


if Http.readystate<>4 then


Exit Function


End If


Gethttppage=http.responsebody


Set Http=nothing


if Err.number<>0 then err. Clear


End Function


obtained the content of the picture to save, give a feeling is to use the FSO to do it, but in fact not, so save the program will be wrong, because the FSO does not support streaming files, so we want to call another object: Ado.strem. The specific process is as follows:


function SaveImage (from,tofile)


Dim Geturl,objstream,imgs


Geturl=trim (from)


imgs=gethttppage (Geturl) ' The process of obtaining the content of a picture


Set objstream = Server.CreateObject ("ADODB. Stream ")" To create a ADODB.stream object that must be ADO more than 2.5 version


objStream.Type = 1 ' Open in binary mode


objStream.Open


objstream.write IMGs ' writes string contents to buffering


objstream. SaveToFile Server.MapPath (ToFile), 2 '-writes buffered content to a file


objstream. Close () ' Closes object


Set objstream=nothing


End Function


so just use a loop to save all the pictures in the address that you just obtained, as follows:


Arrimg=split (retstr, "| |") Split string, get inside address list


allimg= ""


newimg= ""


for I=1 to UBound (arrimg)


if Arrimg (i) <> "" and InStr (Allimg,arrimg (i)) <1 then ' see if this picture has been downloaded


fname=baseurl&cstr (I&mid (arrimg (i), InStrRev (Arrimg (i), "."))


SaveImage (Arrimg (i), fname) ' Save address function, process see above


allimg=allimg& "| |" &arrimg (i) ' Put the address of the saved picture back together to determine the address to be replaced


newimg=newimg& "| |" &fname ' Put the local address string back up


End If


Next





The third step is to replace the original address. The specific process is the following:





Arrnew=split (newimg, "| |") Get the original picture address list


Arrall=split (allimg, "| |") Get the address list of the pictures that have been saved


for I=1 to UBound (arrnew) ' Execute loop replaces the original address


Strs=replace (Strs,arrall (i), arrnew (i))


Next


Cctv=strs





here, the basic process of this function is this, of course, it can be modified to achieve more functions, such as: plus the size of the picture limit, coupled with the download of pictures on the local computer, so as to avoid duplicate download pictures. At the same time should also see this function is not enough to handle only static picture files, not applicable to the program generated pictures.





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.