分享你我的心得.
共乘一片美好网络.

使IE下载WORD文档

在论坛中有很多人问到怎么在IE中让用户直接下载如WORD,PDF等这些文件呢?

下面是一个ASP解决方案:

文件名:Download.asp

<%
Dim Stream
Dim Contents
Dim FileName
Dim FileExt
Const adTypeBinary = 1
FileName = Request.QueryString("FileName")
if FileName = "" Then
<–ubbcodetab–>    <–/ubbcodetab–>Response.Write "无效文件名."
<–ubbcodetab–>    <–/ubbcodetab–>Response.End
End if
‘ 下面是不希望下载的文件
FileExt = Mid(FileName, InStrRev(FileName, ".") + 1)
Select Case UCase(FileExt)
<–ubbcodetab–>    <–/ubbcodetab–>Case "ASP", "ASA", "ASPX", "ASAX", "MDB"
<–ubbcodetab–>    <–/ubbcodetab–><–ubbcodetab–>    <–/ubbcodetab–>Response.Write "受保护文件,不能下载."
<–ubbcodetab–>    <–/ubbcodetab–><–ubbcodetab–>    <–/ubbcodetab–>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
<–ubbcodetab–>    <–/ubbcodetab–>Response.BinaryWrite Stream.Read(1024 * 64)
Wend
Stream.Close
Set Stream = Nothing
Response.Flush
Response.End
%>

<–/ubbcodetab–>使用:Download.asp?FileName=/Files/MY.doc
把你的DOC文件放到根目录Files下,你也可以放到其它地方了。

<–ubbcodetab–><–/ubbcodetab–>如:
<A HREF="Download.asp?FileName=/Files/MY.doc">点击下载WORD文档</A>

赞(0)
未经允许不得转载:小叶白龙博客 » 使IE下载WORD文档
分享到: 更多 (0)

评论 1005

评论前必须登录!