服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - ASP教程 - asp读取远程文件并保存到本地代码

asp读取远程文件并保存到本地代码

2019-10-16 09:59asp代码网 ASP教程

asp读取远程文件并保存到本地代码

  1. <%  
  2. dim act,ifLogin,loginUrl,loginActUrl,loginData,fileUrl,fileName  
  3. act=request.QueryString("act")  
  4. if(act="do") then   
  5.  ifLogin=Cint(request.QueryString("ifLogin"))  
  6.  loginActUrl=request.Form("loginActUrl")  
  7.  loginData=request.Form("loginData")  
  8.  fileUrl=request.Form("fileUrl")  
  9.  fileName=mid(fileUrl,instrrev(fileUrl,"/")+1)  
  10.  extPos=instrrev(fileName,"?")  
  11.  if(extPos>0) then  
  12.   fileName=left(fileName,extPos-1)  
  13.  end if  
  14.  if(ifLogin=1) then  
  15.   call PostHttpPage(loginActUrl,loginData)  
  16.  end if  
  17.  call DownloadFile(fileUrl,fileName)  
  18. else  
  19. %>  
  20. <form name="form1" method="post" action="?act=do">  
  21.   <p>是否登录:  
  22.     <input name="ifLogin" type="radio" value="1">  
  23.     是  
  24.     <input name="ifLogin" type="radio" value="0" checked>  
  25.     否  </p>  
  26.   <p>登录提交地址:  
  27.     <input name="loginActUrl" type="text" size="50">  
  28.   </p>  
  29.   <p>  
  30.     登录数据:<input name="loginData" type="text" size="50">  
  31.   </p>  
  32.   <p>  
  33.     要保存的远程文件URL:  
  34.     <input name="fileUrl" type="text" size="50">  
  35.   </p>  
  36.   <p>  
  37.     <input type="submit" name="Submit" value="提交">  
  38.     <input type="reset" name="Submit2" value="重写">  
  39.   </p>  
  40. </form>  
  41. <%  
  42. end if  
  43. Function BytesToBstr(body,code)   
  44.         dim objstream   
  45.         set objstream = Server.CreateObject("adodb.stream")   
  46.         objstream.Type = 1   
  47.         objstream.Mode =3   
  48.         objstream.Open   
  49.         objstream.Write body   
  50.         objstream.Position = 0   
  51.         objstream.Type = 2   
  52.         objstream.Charset =code  
  53.         BytesToBstr = objstream.ReadText    
  54.         objstream.Close   
  55.         set objstream = nothing   
  56. End Function   
  57. Function PostHttpPage(loginActUrl,PostData)   
  58.     Dim xmlHttp   
  59.     Dim RetStr        
  60.     Set xmlHttp = CreateObject("Microsoft.XMLHTTP")    
  61.     xmlHttp.Open "POST", loginActUrl, False  
  62.     XmlHTTP.setRequestHeader "Content-Length",Len(PostData)   
  63.     xmlHttp.setRequestHeader "Content-Type""application/x-www-form-urlencoded"     
  64.     xmlHttp.Send PostData   
  65.     If Err.Number <> 0 Then   
  66.         Set xmlHttp=Nothing  
  67.   response.Write("提交登录时出错!提交数据:"&PostData)  
  68.         Exit Function  
  69.     End If  
  70.     PostHttpPage=BytesToBstr(xmlHttp.responseBody,"GB2312")  
  71.     Set xmlHttp = nothing  
  72. End Function  
  73. '''''''''''''''''''''''''''''''''''''''''''  
  74. function DownloadFile(url,filename)  
  75.  Set xml = Server.CreateObject("Msxml2.XMLHTTP") '创建对象  
  76.  
  77.    xml.Open "GET",url,False  
  78.    xml.Send '发送请求      
  79.  
  80.    if Err.Number>0 then   
  81.   Response.Status="404"  
  82.   else  
  83.   Response.ContentType="application/octet-stream"  
  84.   Response.AddHeader "Content-Disposition:","attachment; filename=" & filename  
  85.   Range=Mid(Request.ServerVariables("HTTP_RANGE"),7)  
  86.   if Range="" then  
  87.    Response.BinaryWrite(xml.responseBody)  
  88.   else  
  89.    S.position=Clng(Split(Range,"-")(0))  
  90.    Response.BinaryWrite(xml.responseBody)  
  91.   End if  
  92.   end if  
  93.  Response.End    
  94.    Set xml = Nothing  
  95. end function  
  96. '''''''''''''''''''''''''''''''''''''''''''''''''''''  
  97. %> 

延伸 · 阅读

精彩推荐