ReSaveRemoteFile函數之asp實現尋找檔案儲存替換的代碼

來源:互聯網
上載者:User

'================================================
'函數名:ReSaveRemoteFile
'作 用:尋找檔案儲存替換
'參 數:Str ----原字串
'參 數:url ----當然網站URL
'參 數:Dir -----儲存目錄
'參 數:InSave ------是否儲存,True,False
'傳回值:格式化取後的字串
'================================================
Public Function ReSaveRemoteFile(ByVal str, ByVal URL, ByVal Dir,InSave)
Dim s_Content
Dim re
Dim ContentFile, ContentFileUrl
Dim strTempUrl,strFileUrl,DirTemp,PathTemp,FileTemp,Tempi,TempUrlArray,Arr_Path
Dim sAllowExtName
sAllowExtName="rm|swf"

s_Content = str
On Error Resume Next
Set re = New RegExp
re.IgnoreCase = True
re.Global = True
re.Pattern = "((src=|href=)((\S)+[.]{1}(" & sAllowExtName & ")))"
Set ContentFile = re.Execute(s_Content)
Dim sContentUrl(), n, i, bRepeat
n = 0
For Each ContentFileUrl In ContentFile
strFileUrl = Replace(Replace(Replace(Replace(ContentFileUrl.Value, "src=", "", 1, -1, 1), "href=", "", 1, -1, 1), "'", ""), Chr(34), "")
If n = 0 Then
n = n + 1
ReDim sContentUrl(n)
sContentUrl(n) = strFileUrl
Else
bRepeat = False
For i = 1 To UBound(sContentUrl)
If UCase(strFileUrl) = UCase(sContentUrl(i)) Then
bRepeat = True
Exit For
End If
Next
If bRepeat = False Then
n = n + 1
ReDim Preserve sContentUrl(n)
sContentUrl(n) = strFileUrl
End If
End If
Next
If n = 0 Then
ReSaveRemoteFile = s_Content
Exit Function
End If
For i = 1 To n
strTempUrl = sContentUrl(i) : strTempUrl = FormatRemoteUrl(strTempUrl,URL)'得到檔案地址
Response.Write(strTempUrl)
IF InSave=True then
Arr_Path=Split(Dir,"/")
'----------建目錄-----------------------
For Tempi=0 To Ubound(Arr_Path)
If Tempi=0 Then
PathTemp=Arr_Path(0) & "/"
ElseIf Tempi=Ubound(Arr_Path) Then
Exit For
Else
PathTemp=PathTemp & Arr_Path(Tempi) & "/"
End If
If CheckDir(PathTemp)=False Then
If MakeNewsDir(PathTemp)=False Then
SaveTf=False
Exit For
End If
End If
Next
'------------------------------------------------------
TempUrlArray=Split(strTempUrl,"/")
'----------檢查檔案是否存在.如果存在換檔案名稱------------------
Do while True
FileTemp=Dir & MakeRandom(5) & TempUrlArray(Ubound(TempUrlArray))'產生隨機檔案名稱
If CheckFile(FileTemp)=False then
Exit Do
end if
loop
'-------------------------------------------------------------------
Response.Write(FileTemp)
If SaveRemoteFile(FileTemp,strTempUrl)=True then
Response.Write("儲存成功")&"<Br>"
s_Content = Replace(s_Content,sContentUrl(i),FileTemp, 1, -1, 1)'替換地址
Else
Response.Write("儲存失敗")&"<Br>"
End if
Else
s_Content = Replace(s_Content,sContentUrl(i),strTempUrl, 1, -1, 1)'替換地址
End If
Next
Set re = Nothing
PictureExist = True
ReSaveRemoteFile = s_Content
Exit Function
End Function

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.