Nowadays, WEB-based HTML editors are used more and more widely in news systems and article systems. The original style can be maintained once a network is pasted, and pictures can also be maintained in this. However, during use, if the pasted picture is deleted, a big "X" will be left on the face, which affects the appearance. In the past, I had to save this image and then upload it to the server again, which was really troublesome. Can the server automatically download the image, save it on the server, and replace the link on the page? The answer is yes.
Three steps are required to implement this function:
1. Get the address of the original picture. There are many methods, you can use splitting strings, or you can use regular matching. Practice has proven that regular matching is the simplest. The addresses of the analyzed images are saved 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'Ignore case
objRegExp.Global = True' set to 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 implemented. The latter function.
strs=trim(str)
Set Matches =objRegExp.Execute(strs)'Start executing configuration
For Each Match in Matches
RetStr = RetStr &getimgs( Match.Value )'Perform the second round of matching
All images in
Next
have src="http://image address", so you can get the exact image address like this:
function getimgs(str)
getimgs=""
Set objRegExp = New Regexp
objRegExp.IgnoreCase = True
objRegExp.Global = True
objRegExp.Pattern = " http://.+?"""' Take out the address inside
set mm=objRegExp.Execute(str)
For Each Match in mm
getimgs=getimgs&"||"&left(Match.Value,len(Match.Value)-)' string the addresses inside for later use
next
The end function
has obtained the addresses of all pictures, and we can proceed to the second step.
Second, download the image and save it on the server. This can be divided into two steps: one is to obtain the content of the image, and the other is to save it on the server. Obtaining the content of the image is achieved through the following function:
function getHTTPage(url)
on error resume next
dim http
set http=server.createobject("MSXML.XMLHTTP")'Use xmlhttp method to get the content of the image
Http.open "GET",url,false
Http.send()
if Http.readystate<> then
exit function
end if
getHTTPPage=Http.responseBody
set http=nothing
if err.number<>0 then err.Clear
end function
After obtaining the content of the picture, we need to save it. It gives people the impression that it can be done with FSO, but in fact it is not possible. 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, you must have ADO. or above.
objStream.Type ='Open in binary mode
objStream.Open
objstream.write imgs' writes the string content into the buffer
objstream.SaveToFile server.mappath(tofile),'-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(retstr,"||")'Split the string and get the address list inside
allimg=""
newimg=""
for i= to ubound(arrimg)
if arrimg(i)<>"" and instr(allimg,arrimg(i))< then' Check whether 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 the addresses of the saved pictures together to determine the address to be replaced
newimg=newimg&"||"&fname' string the local address back together
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= to ubound(arrnew)' executes a loop to replace the original address
strs=replace(strs,arrall(i),arrnew(i))
next
cctv=strs
Having said that, the basic process of this function is like this. Of course, it can be modified to achieve more functions, such as: adding restrictions on image size and adding restrictions on image downloads on the local machine to avoid duplication. Download image. At the same time, it should also be noted that the shortcoming of this function is that it can only process static image files and cannot be used for images generated by the program.