使用XMLHttp和ADODB.Stream取得远程文件并保存到本地

<%
'****************************************************************************
'PageName:GetRemoteFiles.asp
'Function:Download the files to Server
'Author:xiaotian
'Last Modified at:2003-3-19
'****************************************************************************

'取得远程文件并保存到本地
Function GetRemoteFiels(RemotePath, LocalPath, FileName)
Dim strBody
Dim FilePath

    On Error Resume Next

    '取得流
 strBody = GetBody(RemotePath)
 '取得保存的文件名
 if Right(LocalPath, 1) <> "/" then LocalPath = LocalPath & "/"
 FilePath = LocalPath & GetFileName(RemotePath, FileName)
 '保存文件
 if SaveToFile(strBody, FilePath) = true and err.Number = 0 then
     GetRemoteFiles = true
 else
     GetRemoteFiles = false
 end if

End Function

'远程获取内容
Function GetBody(url)
Dim Retrieval
    '建立XMLHTTP对象
    Set Retrieval = CreateObject("Microsoft.XMLHTTP")
    With Retrieval
        .Open "Get", url, False, "", ""
        .Send
        GetBody = .ResponseBody
    End With
    Set Retrieval = Nothing
End Function

'重组文件名
Function GetFileName(RemotePath, FileName)
Dim arrTmp
Dim strFileExt
    arrTmp = Split(RemotePath, ".")
 strFileExt = arrTmp(UBound(arrTmp))
    GetFileName = FileName & "." & strFileExt
End Function

'将流内容保存为文件
Function SaveToFile(Stream, FilePath)
Dim objStream

    On Error Resume Next

    '建立ADODB.Stream对象,必须要ADO 2.5以上版本
    Set objStream = Server.CreateObject("ADODB.Stream")
    objStream.Type = 1  '以二进制模式打开
    objStream.Open
    objstream.write Stream
    objstream.SaveToFile FilePath, 2
    objstream.Close()
    '关闭对象,释放资源
    Set objstream = Nothing

 if err.Number <> 0 then
     SaveToFile = false
 else
     SaveToFile = true
 end if
End Function
%>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值