通过URL导入图像的快速有效方法是什么?

时间:2011-08-27 22:08:47

标签: asp-classic msxml

我会使用MSXML并导入为二进制文件吗?或者还有另一种更有效的方法吗?

有JPEG的演出和演出。

2 个答案:

答案 0 :(得分:1)

我以前写过一些东西,下面的代码会将远程图像保存在服务器磁盘上。它是经典的ASP并且非常高效:

<% 
Const CONTENT_FOLDER_NAME = "StoredContents"
Dim strImageUrl
strImageUrl = "http://www.gravatar.com/avatar/8c488f9c3d3da5bb756507179a3d53fd?s=32&d=identicon&r=PG"

Call SaveOnServer(strImageUrl, "bill_avatar.jpg")

Sub SaveOnServer(url, strFileName)
    Dim strRawData, objFSO, objFile
    Dim strFilePath, strFolderPath, strError

    strRawData = GetBinarySource(url, strError)
    If Len(strError)>0 Then
        Response.Write("<span style=""color: red;"">Failed to get binary source. Error:<br />" & strError & "</span>")
    Else  
        strFolderPath = Server.MapPath(CONTENT_FOLDER_NAME)
        Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
        If Not(objFSO.FolderExists(strFolderPath)) Then
            objFSO.CreateFolder(strFolderPath)
        End If

        If Len(strFileName)=0 Then
            strFileName = GetCleanName(url)
        End If

        strFilePath = Server.MapPath(CONTENT_FOLDER_NAME & "/" & strFileName)
        Set objFile = objFSO.CreateTextFile(strFilePath)
        objFile.Write(RSBinaryToString(strRawData))
        objFile.Close
        Set objFile = Nothing
        Set objFSO = Nothing

        Response.Write("<h3>Stored contents of " & url & ", total of <span style=""color: blue;"">" & LenB(strRawData) & "</span> bytes</h3>")
        Response.Write("<a href=""" & CONTENT_FOLDER_NAME & "/" & strFileName & """ target=""_blank""><span style=""color: blue;"">" &_
            strFileName & "</span></a>")
    End If
End Sub

Function RSBinaryToString(xBinary)
    ''# Antonin Foller, http://www.motobit.com
    ''# RSBinaryToString converts binary data (VT_UI1 | VT_ARRAY Or MultiByte string)
    ''# to a string (BSTR) using ADO recordset

    Dim Binary
    '' #MultiByte data must be converted To VT_UI1 | VT_ARRAY first.
    If vartype(xBinary)=8 Then Binary = MultiByteToBinary(xBinary) Else Binary = xBinary

    Dim RS, LBinary
    Const adLongVarChar = 201
    Set RS = CreateObject("ADODB.Recordset")
    LBinary = LenB(Binary)

    If LBinary>0 Then
        RS.Fields.Append "mBinary", adLongVarChar, LBinary
        RS.Open
        RS.AddNew
        RS("mBinary").AppendChunk Binary 
        RS.Update
        RSBinaryToString = RS("mBinary")
    Else  
        RSBinaryToString = ""
    End If
End Function

Function MultiByteToBinary(MultiByte)
    ''# © 2000 Antonin Foller, http://www.motobit.com
    ''# MultiByteToBinary converts multibyte string To real binary data (VT_UI1 | VT_ARRAY)
    ''# Using recordset
    Dim RS, LMultiByte, Binary
    Const adLongVarBinary = 205
    Set RS = CreateObject("ADODB.Recordset")
    LMultiByte = LenB(MultiByte)
    If LMultiByte>0 Then
        RS.Fields.Append "mBinary", adLongVarBinary, LMultiByte
        RS.Open
        RS.AddNew
        RS("mBinary").AppendChunk MultiByte & ChrB(0)
        RS.Update
        Binary = RS("mBinary").GetChunk(LMultiByte)
    End If
    MultiByteToBinary = Binary
End Function

Function GetBinarySource(url, ByRef strError)
    Dim objXML
    Set objXML=Server.CreateObject("Microsoft.XMLHTTP")
    GetBinarySource=""
    strError = ""
    On Error Resume Next
        objXML.Open "GET", url, False
        objXML.Send
        If Err.Number<>0 Then
            Err.Clear
            Set objXML = Server.CreateObject("MSXML2.ServerXMLHTTP")
            objXML.Open "GET", url, False
            objXML.Send
            If Err.Number<>0 Then
                strError = "Error " & Err.Number & ": " & Err.Description
                Err.Clear
                Exit Function
            End If
         End If
    On Error Goto 0
    GetBinarySource=objXML.ResponseBody
    Set objXML=Nothing
End Function

Function GetCleanName(s)
    Dim result, x, c
    Dim arrTemp

    arrTemp = Split(s, "/")
    If UBound(arrTemp)>0 Then
        For x=0 To UBound(arrTemp)-1
            result = result & GetCleanName(arrTemp(x)) & "_"
        Next
        result = result & GetPageName(s)
    Else  
        For x=1 To Len(s)
            c = Mid(s, x, 1)
            If IsValidChar(c) Then
                result = result & c
            Else  
                result = result & "_"
            End If
        Next
    End If
    Erase arrTemp
    GetCleanName = result
End Function

Function IsValidChar(c)
    IsValidChar = (c >= "a" And c <= "z") Or (c >= "A" And c <= "Z") Or (IsNumeric(c))
End Function


Function GetPageName(strUrl)
    If Len(strUrl)>0 Then
        GetPageName=Mid(strUrl, InStrRev(strUrl, "/")+1, Len(strUrl))
    Else  
        GetPageName=""
    End If
End Function
%>

只需调用SaveOnServer子例程传递URL和所需的文件名,您也可以省略文件名,在这种情况下,文件名将从URL本身中获取。
服务器文件夹定义为常量,与.asp文件位于同一位置。

答案 1 :(得分:0)

以下是如何在脚本中下载和保存文件的要点: -

 Function DownloadAndSave(sourceUrl, destinationFile)

     Dim req : Set req = CreateObject("WinHttp.WinHttpRequest.5.1")
     req.Open "GET", sourceUrl, false
     req.Send

     Dim stream : Set stream = CreateObject("ADODB.Stream")
     stream.Type = 1 ''# adTypeBinary
     stream.Open
     stream.Write req.ResponseBody
     stream.SaveToFile destinationFile, 2
     stream.Close

 End Function