下载任何文件(尤其是IE关联打开的)问题?

cgq210 2003-11-20 11:07:59
我用了下面的方法,但是下载的时候老是下载该页面
而不是下载我的文件,请问
还有其他的下载方法吗?
请高手帮忙!!!!

下载任何文件(尤其是IE关联打开的)
<%
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
%>


...全文
65 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
cgq210 2003-11-20
  • 打赏
  • 举报
回复
没有反映啊!只看见状态栏慢慢走
编程最光荣 2003-11-20
  • 打赏
  • 举报
回复
download.asp

<%

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

downitem=request.QueryString("downitem")

Dim path, mimeType, sucess

'Server.MapPath(path)

path = Server.MapPath(downitem)

mimeType = "application/x-msdownload"

success = TransferFile(path, mimeType,downitem)

Response.End

%>


调用:


<a href="download.asp?downitem=aa.ppt">aa.ppt</a>

cgq210 2003-11-20
  • 打赏
  • 举报
回复
Set objFile = objFileSystem.GetFile(Path)

好象是这个的问题
我把path都打印出来了
是e:/dfd.swf
没有问题
会是什么原因了?请大虾们帮忙,谢谢了
monkeyhjl 2003-11-20
  • 打赏
  • 举报
回复
alexzhang00(三角猫(sjcatsoft))的很好用呀!
cgq210 2003-11-20
  • 打赏
  • 举报
回复
我的qq:14262285
在线等!
cgq210 2003-11-20
  • 打赏
  • 举报
回复
版本是2.6的
还是不可以
而且不出现任何错误提示
帮帮忙啊!谢谢
DeltaCat 2003-11-20
  • 打赏
  • 举报
回复
不好意思,最后漏了一个 "
调用:
<%
Call DownLoadFromFile("c:\a.ppt","a.ppt")
%>

-----------------------
得到ADO版本:
--------------------
<%
Set ado = Server.CreateObject("ADODB.Connection")
response.write ado.Version
set ado = nothing
%>
DeltaCat 2003-11-20
  • 打赏
  • 举报
回复
Sub DownLoadFromFile(FilePath,FileName)
Dim adS
If Trim(FilePath) = "" or Right(FilePath,1) = "\" then Exit Sub
Set adS = Server.CreateObject("ADODB.Stream")
With adS
.Mode = 3
.Type = 1
.Open
.LoadFromFile FilePath
End With
Response.Buffer = true
Response.Clear
Response.AddHeader "Content-Disposition","attachment;filename=" & FileName
Response.AddHeader "Content-Length",adS.Size
Response.CharSet = "UTF-8"
Response.ContentType = "Application/Octet-Stream"
Response.BinaryWrite adS.Read
Response.Flush
adS.Close
Set adS = nothing
End Sub

调用:
<%
Call DownLoadFromFile("c:\a.ppt","a.ppt)
%>
cgq210 2003-11-20
  • 打赏
  • 举报
回复
怎么看ado版本了?
我刚接触asp,谢谢
编程最光荣 2003-11-20
  • 打赏
  • 举报
回复
1、查看文件夹权限
2、查看是否支持FileSystemObject组件
3、Ado2.5以上版本

28,407

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧