aspでリモートファイルをローカルファイルに置換してリモートファイルを保存するコードを実現します。
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
%>
、呼び出し方法があります。include file=「remote.asp」--> 文章の入庫箇所を下記のコードに変更します。
If sSaveFileSelect=True Then
Rs("Content")=ReplaceRemoteUrl(ArticleContent,sSaveFilePath,sFileExt)
Else
Rs("Content")=ArticleContent
End If