Automatically resolve picture addresses in Web pages with ASP and save them to local server

Source: Internet
Author: User

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'设置配置对象
objRegExp.IgnoreCase = True’忽略大小写
objRegExp.Global = True’设置为全文搜索
objRegExp.Pattern = "标签,然后再取出里面的图片地址后面的getimgs函数就是实现后一个功能的。
strs=trim(str)
Set Matches =objRegExp.Execute(strs)’开始执行配置
For Each Match in Matches
?RetStr = RetStr &getimgs( Match.Value )’执行第二轮的匹配
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://.+?"""’取出里面的地址
set mm=objRegExp1.Execute(str)
For Each Match1 in mm
getimgs=getimgs&"||"&left(Match1.Value,len(Match1.Value)-1)’把里面的地址串起来备用
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")‘使用xmlhttp的方法来获得图片的内容
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:

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.