Implement code to replace remote files as local files and save remote files under ASP application tips

Source: Internet
Author: User
Tags file url save file
1, the following text file download, and will. TXT changed to remote.asp, there is a specific set of methods
Copy Code code as follows:

<%
' Save remote pictures when adding resources
Const Ssavefileselect=true

' Remote picture save directory, end please do not add '/'
Const ssavefilepath= "/images/news"

' Remote picture Save type
Const sfileext= "Jpg|gif|bmp|png"

'/////////////////////////////////////////////////////
' Function: Replace the remote file in the string as a local file and save the remote file
Parameters
' SHTML: string to replace
' Ssavepath: Path to save file
' SEXT: The extension that performs the substitution
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 (replace (Remotefileurl, "/", "a"), ":", "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

'////////////////////////////////////////
' Function: Save remote files to local
' Parameter: localfilename------local filename
' 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 With
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 With
Set ads=nothing
End Sub

'////////////////////////////////////////
' Function: Check if the component is installed
' parameter: strclassstring----Component Name
' Return value: True----already 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. Calling method:
<!--#include file= "remote.asp"-->

The place where the article is stored is changed to the following code
Copy Code code 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.