asp下實現替換遠程檔案為本地檔案並儲存遠程檔案的代碼

來源:互聯網
上載者:User

1、將下面的文字檔下載,並將.TXT改為remote.asp,裡面有具體設定方法 複製代碼 代碼如下:<%
'添加資源時是否儲存遠程圖片
Const sSaveFileSelect=True

'遠程圖片儲存目錄,結尾請不要加“/”
Const sSaveFilePath="/images/News"

'遠程圖片檔案類型
Const sFileExt="jpg|gif|bmp|png"

'/////////////////////////////////////////////////////
'作 用:替換字串中的遠程檔案為本地檔案並儲存遠程檔案
'參 數:
' sHTML : 要替換的字串
' sSavePath : 儲存檔案的路徑
' sExt : 執行替換的副檔名
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

'////////////////////////////////////////
'作 用:儲存遠端檔案到本地
'參 數:LocalFileName ------ 本地檔案名稱
' RemoteFileUrl ------ 遠程檔案URL
'傳回值:True ----成功
' False ----失敗
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

'////////////////////////////////////////
'作 用:檢查組件是否已經安裝
'參 數:strClassString ----組件名
'傳回值:True ----已經安裝
' False ----沒有安裝
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、調用方法:
<!--#include file="remote.asp"-->

文章入庫的地方改成下面的代碼 複製代碼 代碼如下:If sSaveFileSelect=True Then
Rs("Content")=ReplaceRemoteUrl(ArticleContent,sSaveFilePath,sFileExt)
Else
Rs("Content")=ArticleContent
End If

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.