TXT.WORD文档下载在本地,而不是在浏览器中打开

本文介绍了一种使用ASP实现文件在线下载的方法。该方法通过识别不同的文件类型来设置正确的MIME类型,确保用户能够顺利下载所需的文件。文章还展示了如何通过编程获取文件大小并设置HTTP响应头。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<%
Response.Buffer=true
取得要下载的文件,如果不为空。就下载
dim url,trueurl
url="下载文件路径和下载文件名"
   fname=Request("filename")
   if fname<>"" then
      trueurlurl=server.MapPath("/") &url
   end if
   set objFso=server.CreateObject("scripting.filesystemobject")
   set fn=objFso.GetFile(trueurl)
   flsize=fn.size
   flname=fn.name
   set fn=nothing
   set objFso=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 ".ASP", ".ASA", "ASPX", "ASAX", ".MDB"
        Response.Write "受保护文件,不能下载."
        Response.End
      case else
      ContentType="appliation/octet-stream"
   end select

   Response.AddHeader "Content-Disposition", "attachment; filename="&flname
   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
%>
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值