ASP Stream文件流下载程序源码
<%DimfullName,s,fs,fl,Filesize,Filename,currentPath,relativePath'取得文件名参数,可以是表单提交或者查询字符串fullName=Request.QueryString("down")IffullName=""ThenCallShowMessage("对不起,文件名为空!")'转为绝对路径'该门课件的根路径currentPath=Request.ServerVariables("PATH_TRANSLATED")currentPath=Replace(currentPath,"script/download.asp","")'网站子路径,为空代表下载文件直接放在根路径,可以添加子路径如"down/"relativePath="stream_db/"'下载文件的完全路径Filename=currentPath&relativePath&fullName'检查文件是否存在Setfs=Server.CreateObject("Scripting.FileSystemObject")'IfNotfs.FileExists(Filename)ThenCallShowMessage("对不起,指定文件不存在!")'取得文件大小,单位是字节Setfl=fs.GetFile(Filename)Filesize=fl.Size'销毁FSO对象Setfl=NothingSetfs=Nothing'清理缓存Response.Buffer=TrueResponse.Clear'创建Stream对象Sets=Server.CreateObject("ADODB.Stream")s.Open'设置为二进制方式s.Type=1'容错OnErrorResumeNext'装载文件s.LoadFromFile(Filename)IfErrThenCallShowMessage("装载指定文件出现未知错误!")'向浏览器输出头部Response.AddHeader"Content-Disposition","attachment;filename="&fullNameResponse.AddHeader"Content-Length",Filesize'Response.CharSet="UTF-8"Response.ContentType="application/octet-stream"'分段向浏览器输出文件DoWhileNots.EOSContents=s.Read(4096)'每次读取4096KBResponse.BinaryWriteContentsResponse.FlushLoop'一次性向浏览器输出文件'Response.BinaryWrites.Read'Response.Flush'销毁对象s.Close:Sets=Nothing'在本页输出提示信息SubShowMessage(msg)Response.Write"<br><divalign='center'><divstyle='color:red;font-weight:bold;text-align:center;border:1pxsolid#CCCCCC;'background-color:#E8E8E8;padding:4px2px2px;width:300px;font-size:12px'>"&msg&"</div></div><br>"Response.EndEndSub%>。
2019/10/31