文章分类 Classification
文件下载方法
稿件来源: 阳光企业网站管理系统 撰稿作者: 太阳光 发表日期: 2011-11-05 阅读次数: 105 查看权限: 游客查看
在做文件下载时,网页会自动打开下载文件而不是另保存方式,比如word文档。怎么办?
在做文件下载时,网页会自动打开下载文件而不是另保存方式,比如word文档。怎么办?
我们提供两种方法参考:
<% Dim Stream Dim Contents Dim FileName Dim FileExt Const adTypeBinary = 1 FileName = Request.QueryString("FileName") if FileName = "" Then Response.Write "无效文件名." Response.End End if ' 下面是不希望下载的文件 FileExt = Mid(FileName, InStrRev(FileName, ".") + 1) Select Case UCase(FileExt) Case "ASP", "ASA", "ASPX", "ASAX", "MDB" Response.Write "受保护文件,不能下载." Response.End End Select ' 下载这个文件 Response.Clear Response.ContentType = "application/octet-stream" Response.AddHeader "content-disposition", "attachment; filename=" & FileName Set Stream = server.CreateObject("ADODB.Stream") Stream.Type = adTypeBinary Stream.Open Stream.LoadFromFile Server.MapPath(FileName) While Not Stream.EOS Response.BinaryWrite Stream.Read(1024 * 64) Wend Stream.Close Set Stream = Nothing Response.Flush Response.End %>
使用:Download.asp?FileName=/Files/MY.doc
把你的DOC文件放到根目录Files下,你也可以放到其它地方了。
如:
<A HREF="Download.asp?FileName=/Files/MY.doc">点击下载WORD文档</A>
=====================================方法二:流下载===============================
<% Response.Buffer = true Response.Clear dim url Dim fso,fl,flsize dim Dname Dim objStream,ContentType,flName,isre,url1 '*********************************************调用时传入的下载文件名 Dname=trim(request("n")) upname=trim(request("upname")) If Dname<>"" Then '******************************下载文件存放的服务端目录 url=server.MapPath("doc/")&"\"&Dname '“admin/UploadInfor/”为路径名 End If Set fso=Server.CreateObject("Scripting.FileSystemObject") Set fl=fso.getfile(url) flsize=fl.size flName=fl.name Set fl=Nothing Set fso=Nothing Set objStream = Server.CreateObject("ADODB.Stream") objStream.Open objStream.Type = 1 objStream.LoadFromFile url Select Case lcase(Right(flName, 4)) Case ".asf" ContentType = "video/x-ms-asf" Case ".avi" ContentType = "video/avi" Case ".doc" ContentType = "application/msword" Case ".zip" ContentType = "application/zip" Case ".xls" ContentType = "application/vnd.ms-excel" Case ".gif" ContentType = "image/gif" Case ".jpg", "jpeg" ContentType = "image/jpeg" Case ".wav" ContentType = "audio/wav" Case ".mp3" ContentType = "audio/mpeg3" Case ".mpg", "mpeg" ContentType = "video/mpeg" Case ".rtf" ContentType = "application/rtf" Case ".htm", "html" ContentType = "text/html" Case ".txt" ContentType = "text/plain" Case Else ContentType = "application/octet-stream" End Select Response.AddHeader "Content-Disposition", "attachment; filename=" & upname Response.AddHeader "Content-Length", flsize Response.Charset = "UTF-8" Response.ContentType = ContentType Response.BinaryWrite objStream.Read Response.Flush response.Clear() objStream.Close Set objStream = Nothing %>
关键词: 下载,流下载 编辑时间: 2011-11-05
0
高兴0
支持0
搞笑0
不解0
谎言0
枪稿0
震惊0
无奈0
无聊0
反对0
愤怒
0%(0)
0%(0)
- 暂无评论
文章图片 article Pictrue
网友评论