Use asp to replace remote files with local files and save the code of remote files

Source: Internet
Author: User
Tags file url

1. download the following text file and change. TXT to remote. asp. Copy codeThe Code is as follows: <%
'Whether to save the remote image when adding a resource
Const sSaveFileSelect = True

'Remote image storage directory. Do not add "/" at the end.
Const sSaveFilePath = "/images/News"

'Remote image storage type
Const sFileExt = "jpg | gif | bmp | png"

'/////////////////////////////////////// //////////////
'Usage: Replace the remote file in the string with a local file and save the remote file.
'Parameter:
'Shtml: string to be replaced
'Ssavepath: Path to save the file
'Sext: the extension to be replaced
Function ReplaceRemoteUrl (sHTML, sSaveFilePath, sFileExt)
Dim s_Content
S_Content = sHTML
If IsObjInstalled ("Microsoft. XMLHTTP") = False then
ReplaceRemoteUrl = s_Content
Exit Function
End If

Dim re, RemoteFile, RemoteFileurl, SaveFileName, SaveFileType, arrSaveFileNameS, arrSaveFileName, sSaveFilePaths
Set re = new RegExp
Re. IgnoreCase = True
Re. Global = True
Re. pattern = "(http | https | ftp | rtsp | mms) :( \// |\\\\) {1} (\ w) + [.]) {1,} (net | com | cn | org | cc | TV | [0-9] {1, 3}) (\ S * \/) (\ S) + [.] {1} ("& sFileExt &")))"
Set RemoteFile = re. Execute (s_Content)
For Each RemoteFileurl in RemoteFile
SaveFileType = Replace (RemoteFileurl, "/", "a"), ":", "")
ArrSaveFileName = Right (SaveFileType, 12)
SSaveFilePaths = sSaveFilePath &"/"
SaveFileName = sSaveFilePaths & arrSaveFileName
Call SaveRemoteFile (SaveFileName, RemoteFileurl)
S_Content = Replace (s_Content, RemoteFileurl, SaveFileName)
Next
ReplaceRemoteUrl = s_Content
End Function

'////////////////////////////////////////
'Usage: Save the remote file to the local device.
'Parameter: LocalFileName ------ local file name
'Remotefileurl ------ Remote File URL
'Return value: True ---- successful
'False ---- failed
Sub SaveRemoteFile (s_LocalFileName, s_RemoteFileUrl)
Dim Ads, Retrieval, GetRemoteData
On Error Resume Next
Set Retrieval = Server. CreateObject ("Microsoft. XMLHTTP ")
With Retrieval
. Open "Get", s_RemoteFileUrl, False ,"",""
. Send
GetRemoteData =. ResponseBody
End
Set Retrieval = Nothing
Set Ads = Server. CreateObject ("Adodb. Stream ")
With Ads
. Type = 1
. Open
. Write GetRemoteData
. SaveToFile Server. MapPath (s_LocalFileName), 2
. Cancel ()
. Close ()
End
Set Ads = nothing
End Sub

'////////////////////////////////////////
'Usage: Check whether the component has been installed
'Parameter: strClassString ---- component name
'Return value: True ---- installed
'False ---- Not Installed
Function IsObjInstalled (s_ClassString)
On Error Resume Next
IsObjInstalled = False
Err = 0
Dim xTestObj
Set xTestObj = Server. CreateObject (s_ClassString)
If 0 = Err Then IsObjInstalled = True
Set xTestObj = Nothing
Err = 0
End Function
%>

2. Call method:
<! -- # Include file = "remote. asp" -->

Change the following code to the place where the article is storedCopy codeThe Code is as follows: If sSaveFileSelect = True Then
Rs ("Content") = ReplaceRemoteUrl (ArticleContent, sSaveFilePath, sFileExt)
Else
Rs ("Content") = ArticleContent
End If

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.