Web page
Now based on the Web page of the HTML editor in the news system, the article system more and more widely used, a Web page can maintain the original style, while the picture can also 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.
Three steps are required to implement this feature:
One, get the address of the picture in 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 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 = " 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
? Retstr = Retstr &getimgs (match.value) ' performs a second-round match
Next
All the pictures inside are such 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
With all the pictures in the address, we can do the second step of the operation.
Second, 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 contents of the picture are achieved by using the following function:
function gethttppage (URL)
On Error Resume Next
Dim http
Set Http=server.createobject ("Msxml2.xmlhttp") ' uses XMLHTTP method to get the contents of a 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 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 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 ' 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
Cctv=strs
In this case, 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 picture download on the local computer restrictions, so as not to create a 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.