Use asp to automatically parse the image address in the webpage and save it to the local server.

Source: Internet
Author: User
Topic: use asp to automatically parse the image address on the webpage and save it to the local server.

 

Currently, HTML editors based on Web pages are widely used in news systems and document systems. A webpage can stick to the original style, and images can also be kept on this page. However, if the image on the pasted page is deleted, a large "X" will be left on the page, affecting the appearance. In the past, I had to save the image and upload it to the server again. This is really troublesome. Can I have the server automatically download images stored on the server and replace the links on the page? The answer is yes.
To implement this function, three steps are required:
1. Obtain the image address on the original page. There are many methods, which can be used to separate strings or use regular expressions for matching. Practice has proved that regular expression matching is the simplest. The addresses of the analyzed images are saved in the label. We can obtain all the tags first. The process is as follows:
Set objRegExp = New Regexp 'to Set the configuration object
ObjRegExp. IgnoreCase = true' case insensitive
ObjRegExp. Global = true' is set to full-text search.
ObjRegExp. Pattern = " "'To ensure that the image address can be accurately retrieved, there are two layers of configuration: First, find the label in it, then, the getimgs function behind the image address is used to implement the next function.
Strs = trim (str)
Set Matches = objRegExp. Execute (strs )'
For Each Match in Matches
RetStr = RetStr & getimgs (Match. Value )'
Next

All the images are in the src = "http: // image address", so you can get the exact image address as follows:
Function getimgs (str)
Getimgs = ""
Set objRegExp1 = New Regexp
ObjRegExp1.IgnoreCase = True
ObjRegExp1.Global = True
ObjRegExp1.Pattern = "http: //. +? "'Retrieve the address
Set mm = objRegExp1.Execute (str)
For Each mattings in mm
Getimgs = getimgs & "|" & left (Match1.Value, len (Match1.Value)-1) 'Concatenates the addresses in the string for backup.
Next
End function

After obtaining the addresses of all images, we can perform Step 2 operations.
2. Download the image and save it on the server. This can be divided into two steps: one is to get the image content, and the other is to save it on the server. The following function is used to obtain the image content:
Function getHTTPPage (url)
On error resume next
Dim http
Set http = server. createobject ("MSXML2.XMLHTTP") 'uses the xmlhttp method to obtain the image content
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
After obtaining the image content, you can use FSO to save it. However, in fact, it does not work. In this way, an error occurs when saving the program because FSO does not support streaming files, so we need 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 rest content of an image
Set objStream = Server. CreateObject ("ADODB. Stream") 'creates an ADODB. Stream object, which must be of version ADO 2.5 or later.
ObjStream. Type = 1' open in binary mode
ObjStream. Open
Objstream. write imgs write string content to buffer
Objstream. SaveToFile server. mappath (tofile), 2'-Write the buffered content to the file
Objstream. Close () 'Close the object
Set objstream = nothing
End function
Therefore, you only need to use a loop to save all the images in the obtained address. The specific process is as follows:
Arrimg = split (retstr, "|") 'splits the string to obtain the address list.
Allimg = ""
Newimg = ""
For I = 1 to ubound (arrimg)
If arrimg (I) <> "" and instr (allimg, arrimg (I) <1 then ', check whether the image has been downloaded.
Fname = baseurl & cstr (I & mid (arrimg (I), Rev (arrimg (I ),".")))
Saveimage (arrimg (I), fname) 'specifies the function for saving the address. For details about the process, see the preceding section.
Allimg = allimg & "|" & arrimg (I) 'returns the address string of the saved image to determine the address to be replaced.
Newimg = newimg & "|" & fname' returns the local address string
End if
Next
The third step is to replace the original address. The specific process is as follows:
Arrnew = split (newimg, "|") 'to obtain the original image address list
Arrall = split (allimg, "|") 'obtains the address list of saved images.
For I = 1 to ubound (arrnew) ', execute the loop to replace the original address
Strs = replace (strs, arrall (I), arrnew (I ))
Next
Cctv = strs
Here, the basic process of this function is like this. Of course, you can modify it to implement more functions, such as the limit on the size of a part, add restrictions on downloading images on the local machine to avoid repeated downloading. The disadvantage of this function is that it can only process static image files and cannot be used to generate images.

If you have any good comments and suggestions, contact me: qq: 26452218. I already have this dll-based program. You can also contact me.

[Print this article]
Related Article

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.