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

时间:2021-05-28

<%
dimact,ifLogin,loginUrl,loginActUrl,loginData,fileUrl,fileName
act=request.QueryString("act")
if(act="do")then
ifLogin=Cint(request.QueryString("ifLogin"))
loginActUrl=request.Form("loginActUrl")
loginData=request.Form("loginData")
fileUrl=request.Form("fileUrl")
fileName=mid(fileUrl,instrrev(fileUrl,"/")+1)
extPos=instrrev(fileName,"?")
if(extPos>0)then
fileName=left(fileName,extPos-1)
endif
if(ifLogin=1)then
callPostHttpPage(loginActUrl,loginData)
endif
callDownloadFile(fileUrl,fileName)
else
%>
<formname="form1"method="post"action="?act=do">
<p>是否登录:
<inputname="ifLogin"type="radio"value="1">

<inputname="ifLogin"type="radio"value="0"checked>
否</p>
<p>登录提交地址:
<inputname="loginActUrl"type="text"size="50">
</p>
<p>
登录数据:<inputname="loginData"type="text"size="50">
</p>
<p>
要保存的远程文件URL:
<inputname="fileUrl"type="text"size="50">
</p>
<p>
<inputtype="submit"name="Submit"value="提交">
<inputtype="reset"name="Submit2"value="重写">
</p>
</form>
<%
endif
FunctionBytesToBstr(body,code)
dimobjstream
setobjstream=Server.CreateObject("adodb.stream")
objstream.Type=1
objstream.Mode=3
objstream.Open
objstream.Writebody
objstream.Position=0
objstream.Type=2
objstream.Charset=code
BytesToBstr=objstream.ReadText
objstream.Close
setobjstream=nothing
EndFunction
FunctionPostHttpPage(loginActUrl,PostData)
DimxmlHttp
DimRetStr
SetxmlHttp=CreateObject("Microsoft.XMLHTTP")
xmlHttp.Open"POST",loginActUrl,False
XmlHTTP.setRequestHeader"Content-Length",Len(PostData)
xmlHttp.setRequestHeader"Content-Type","application/x-www-form-urlencoded"
xmlHttp.SendPostData
IfErr.Number<>0Then
SetxmlHttp=Nothing
response.Write("提交登录时出错!提交数据:"&PostData)
ExitFunction
EndIf
PostHttpPage=BytesToBstr(xmlHttp.responseBody,"GB2312")
SetxmlHttp=nothing
EndFunction
'''''''''''''''''''''''''''''''''''''''''''
functionDownloadFile(url,filename)
Setxml=Server.CreateObject("Msxml2.XMLHTTP")'创建对象

xml.Open"GET",url,False
xml.Send'发送请求

ifErr.Number>0then
Response.Status="404"
else
Response.ContentType="application/octet-stream"
Response.AddHeader"Content-Disposition:","attachment;filename="&filename
Range=Mid(Request.ServerVariables("HTTP_RANGE"),7)
ifRange=""then
Response.BinaryWrite(xml.responseBody)
else
S.position=Clng(Split(Range,"-")(0))
Response.BinaryWrite(xml.responseBody)
Endif
endif
Response.End
Setxml=Nothing
endfunction
'''''''''''''''''''''''''''''''''''''''''''''''''''''
%>

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章