1. Get the address of the image on the original page.
<%
function PicStr(str)
Set objRegExp = New Regexp 'Set configuration object
objRegExp.IgnoreCase = True 'Ignore case
objRegExp.Global = True 'Set for full text search
objRegExp.Pattern = <IMG.+?> 'In order to ensure that the image address can be retrieved accurately, it is divided into two levels of configuration: first find the <IMG> tag inside, and then retrieve the image address inside. The getimgs function behind it is to implement the latter one Functional.
strs=trim(str)
Set Matches =objRegExp.Execute(strs) 'Start executing configuration
For Each Match in Matches
PicStr = PicStr &getimgs( Match.Value ) 'Perform the second round of matching
Next
'All pictures are like this src=http://picture address, so you can get the exact picture address like this
end function
function getimgs(str)
getimgs=
Set objRegExp1 = New Regexp
objRegExp1.IgnoreCase = True
objRegExp1.Global = True
objRegExp1.Pattern = http://.+? 'Get the address inside
set mm=objRegExp1.Execute(str)
For Each Match1 in mm
getimgs=getimgs&||&left(Match1.Value,len(Match1.Value)-1) 'String together the addresses inside for later use
next
end function
%>
Second, download the image and save it on the server.
<%
function getHTTPage(url)
on error resume next
dim http
set http=server.createobject(MSXML2.XMLHTTP) 'Use xmlhttp method to obtain the content of the image
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
'The content of the image has been obtained and needs to be saved. It gives the impression that it can be done with FSO, but in fact it does not work. In this way, the saving program will error because FSO does not support streaming files, so we have 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 specific content of the image
Set objStream = Server.CreateObject(ADODB.Stream)' To create an ADODB.Stream object, ADO 2.5 or above is required
objStream.Type =1'Open in binary mode
objStream.Open
objstream.write imgs' writes the string content into the buffer
objstream.SaveToFile server.mappath(tofile),2'-write the buffered content to the file
objstream.Close()'Close the object
set objstream=nothing
end function
'So just use a loop to save all the pictures in the address just obtained. The specific process is as follows:
arrimg=split(PicStr(str),||) 'Split the string and get the address list inside
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)' function to save the address, see the process above
allimg=allimg&||&arrimg(i) 'String back the addresses of the saved pictures to determine the address to be replaced
newimg=newimg&||&fname 'String back the local address
end if
next
'The third step is to replace the original address. The specific process is as follows:
arrnew=split(newimg,||) 'Get the original image address list
arrall=split(allimg,||) 'Get the address list of saved pictures
for i=1 to ubound(arrnew) 'Execute a loop to replace the original address
strs=replace(strs,arrall(i),arrnew(i))
next
%>