One, get the address of the picture in the original page.
<%
function Picstr (str)
Set objregexp = New Regexp ' Set Configuration object
Objregexp.ignorecase = True ' ignores case
Objregexp.global = True ' Set to Full-text search
Objregexp.pattern = " 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 the Match in matches
Picstr = Picstr &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
End Function
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
%>
Second, download the picture and save it on the server.
<%
function gethttppage (URL)
On Error Resume Next
Dim http
Set Http=server.createobject ("MSXML2. XMLHTTP ") ' use XMLHTTP method to get the content 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
' Get the content of the picture to save, give a feeling is to use the FSO to do it, but actually 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 buffer
Objstream. SaveToFile Server.MapPath (ToFile), 2 '-writes buffered content to file
Objstream. Close () ' Closes object
Set objstream=nothing
End Function
So just use a loop to save all the pictures in the address you just obtained, as follows:
Arrimg=split (Picstr (str), "| |") ' 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 ' 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 you've saved
For I=1 to UBound (arrnew) ' execution loop replaces the original address
Strs=replace (Strs,arrall (i), arrnew (i))
Next
%>