当前位置: 首页 > 图文教程 > 网络编程 > ASP > 实现文件下载而不是由ie打开的代码

ASP
ASP调用ORACLE存储过程并返回结果集
用ASP实现网页BBS
关于Global.asa文件的深入研究与session变量失效提示的具体方法
简易ASP+注册系统
防护手册:如何防止ASP木马在服务器上运行
用Visual Basic实现多画面播放功能之二
如何增强ASP程序性能(1)
如何增强ASP程序性能(2)
如何增强ASP程序性能(3)
ASP备份数据库
二十八条改善 ASP 性能和外观的技巧
在Form域中Post大于100K的数据
如何使用ASP制作模似动态生长的表单?
Microsoft IIS 真的如此「不安全」吗?(1)
Microsoft IIS 真的如此「不安全」吗?(2)
Microsoft IIS 真的如此「不安全」吗?(3)
Microsoft IIS 真的如此「不安全」吗?(4)
Microsoft IIS 真的如此「不安全」吗?(5)
关于页面和代码分离
ServerVariables 对路径的操作

ASP 中的 实现文件下载而不是由ie打开的代码


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 28 ::
收藏到网摘: n/a

       <%
  Const ForReading=1
  Const TristateTrue=-1 ''Unicode
  Const FILE_TRANSFER_SIZE=16384 ''16k
  
  ''Use the following line for IIS4/PWS - this is the default for IIS5
  Response.Buffer = True
  
  Function TransferFile(path, mimeType, filename)
  Dim objFileSystem, objFile, objStream
  Dim char
  Dim sent
  send=0
  TransferFile = True
  
  Set objFileSystem = Server.CreateObject("Scripting.FileSystemObject")
  Set objFile = objFileSystem.GetFile(Path)
  Set objStream = objFile.OpenAsTextStream(ForReading, TristateTrue)
  
  Response.AddHeader "content-type", mimeType
  response.AddHeader "Content-Disposition","attachment;filename="&filename
  Response.AddHeader "content-length", objFile.Size
  
  Do While Not objStream.AtEndOfStream
   char = objStream.Read(1)
   Response.BinaryWrite(char)
   sent = sent + 1
   If (sent MOD FILE_TRANSFER_SIZE) = 0 Then
   Response.Flush
   If Not Response.IsClientConnected Then
   TransferFile = False
   Exit Do
   End If
   End If
  Loop
  
  Response.Flush
  If Not Response.IsClientConnected Then TransferFile = False
  
  objStream.Close
  Set objStream = Nothing
  Set objFileSystem = Nothing
  End Function
  
  Dim path, mimeType, sucess
  ''Server.MapPath(path)
  path = "C:\Inetpub\wwwroot\help.gif"
  mimeType = "application/x-msdownload"
  sucess = TransferFile(path, mimeType,"help.gif")
  Response.End
  %>