ASP使用BinaryWrite与Adodb.Stream实现下载

因为某种原因要实现一个RAR的文件的下载,而且为了节省服务器资源,需要做一个判断,下载引用方法如下:

   	<input type="button" value="下载工具包" onclick="location.href='http-localhost-api?/easywork-imageupload-client-full/download/port-80/ver=<%=date%/>.gzip.rar';">
   	<input type="button" value="下载升级包" onclick="location.href='http-localhost-api?/easywork-imageupload-client-fast/download/port-80/ver=<%=date%/>.gzip.rar';">

初步打算用FSO读取为TextStream,然后用BinaryWrite输出,代码如下:

< %@codepage=65001%>
< %
response.buffer=false
if session("safe_mode")<>"ok" then response.write "<form action=""../../../admin.asp"" method=""post"" id=""a""><script type=""text/javascript"">document.getElementById(""a"").submit();</script>":response.end
if instr(request.Querystring,"/")< =0 then response.redirect "/":response.end
set fso=server.createobject("Scripting.FileSystemObject")
file=server.mappath(split(request.QueryString,"/")(1)&".rar")
if not fso.fileexists(file) then response.redirect "/":response.end
set fto=fso.getfile(file)
set fts=fto.OpenAsTextStream(1,-1)
 
Response.ContentType = "application/octet-stream"
Response.AddHeader "Content-Disposition","attachment;filename="&split(request.QueryString,"/")(1)&".rar"
 
response.binarywrite fts.readall
%>
</form>

实际运行时速度不是很快,可能是fts.readall的原因吧,于是修改一下代码,一行一行的输出:

< %@codepage=936%>
< %
response.buffer=false
if session("safe_mode")<>"ok" then response.write "<form action=""../../../admin.asp"" method=""post"" id=""a""><script type=""text/javascript"">document.getElementById(""a"").submit();</script>":response.end
if instr(request.Querystring,"/")< =0 then response.redirect "/":response.end
set fso=server.createobject("Scripting.FileSystemObject")
file=server.mappath(split(request.QueryString,"/")(1)&".rar")
if not fso.fileexists(file) then response.redirect "/":response.end
set fto=fso.getfile(file)
set fts=fto.OpenAsTextStream(1,-1)
 
 
Response.ContentType = "application/octet-stream"
Response.AddHeader "Content-Disposition","attachment;filename="&split(request.QueryString,"/")(1)&".rar"
do while not fts.atendofstream
	response.binarywrite fts.readall
loop
%>
</form>

效率有所提高,因为FSO每次读取硬盘,速度很慢~到网上搜索了一下,发现Adodb中除了Connection和Recordset,还有一个Stream,于是有了第三种方法:

< %@codepage=65001%>
< %
response.buffer=false
if session("safe_mode")<>"ok" then response.write "<form action=""../../../admin.asp"" method=""post"" id=""a""><script type=""text/javascript"">document.getElementById(""a"").submit();</script>":response.end
if instr(request.Querystring,"/")< =0 then response.redirect "/":response.end
set fso=server.createobject("Scripting.FileSystemObject")
file=server.mappath(split(request.QueryString,"/")(1)&".rar")
if not fso.fileexists(file) then response.redirect "/":response.end
 
Response.ContentType = "application/octet-stream"
Response.AddHeader "Content-Disposition","attachment;filename="&now&".rar"
 
Set Ados=Server.CreateObject("Adodb.Stream")
Ados.Mode=3
Ados.Type=1
Ados.Open
Ados.LoadFromFile(Server.mappath(split(request.QueryString,"/")(1)&".rar"))
 
response.binarywrite Ados.read()
%>
</form>

速度已经不错了,同时很多人访问CPU占用也不是很高。

ASP使用BinaryWrite与Adodb.Stream实现下载》上有1条评论

发表评论

电子邮件地址不会被公开。 必填项已用*标注