asp读取远程文件并保存到本地代码
2018-09-06 12:05
<%
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=form1method=postaction=?act=do>
<p>是否登录:
<inputname=ifLogintype=radiovalue=1>
是
<inputname=ifLogintype=radiovalue=0checked>
否</p>
<p>登录提交地址:
<inputname=loginActUrltype=textsize=50>
</p>
<p>
登录数据:<inputname=loginDatatype=textsize=50>
</p>
<p>
要保存的远程文件URL:
<inputname=fileUrltype=textsize=50>
</p>
<p>
<inputtype=submitname=Submitvalue=提交>
<inputtype=resetname=Submit2value=重写>
</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.OpenPOST,loginActUrl,False
XmlHTTP.setRequestHeaderContent-Length,Len(PostData)
xmlHttp.setRequestHeaderContent-Type,application/x-
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.OpenGET,url,False
xml.Send发送请求
ifErr.Number>0then
Response.Status=404
else
Response.AddHeaderContent-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
%>