还是关于asp实现下载的功能

randomfeel 2009-08-27 05:00:01
昨天在这里问过一个类似的问题了
http://topic.csdn.net/u/20090826/16/dfcd2c4c-069d-40ad-b014-94e6f1ddd827.html
已解决

现在有新的问题,就是除了传文件路径到 .asp页面,实现下载外
还要传一个文件名过去

如 download.asp?path=/file/dddd.cab&filename=dddd.doc
而download.asp的代码,不能直接实现下载dddd.cab,而是要根据filename里的格式:.doc
把 dddd.cab 转成 dddd.doc 再提供下载,或者直接打开

谢谢~
...全文
69 点赞 收藏 2
写回复
2 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
randomfeel 2009-08-27
谢谢
回复
hookee 2009-08-27

<%
path = Request("path")
fname = Request("filename")
sFilePath = Server.MapPath(path)

Set oSF = CreateObject("Scripting.FileSystemObject")
Set f = oSF.GetFile(sFilePath)
Set oStream = Createobject("ADODB.Stream")
With oStream
.Type = 1
.Mode = 3
.Open
.LoadFromFile sFilePath
End With
Set oSF = Nothing
If Not TransferFile(oStream, "application/octet-stream", fname) Then
response.Write "Error"
End If
oStream.close
Set oStream = Nothing

Function TransferFile(ByRef oStream, sMimeType, sFilename)
Dim iChar, iSent, iSize
Dim c
Const FILE_TRANSFER_SIZE = 32768 '32k

iSent = 0
TransferFile = True
iSize = oStream.Size
With Response
.Charset = "GB2312"
.ContentType= sMimeType
.AddHeader "Accept-Ranges", "bytes"
.AddHeader "Content-Length", iSize
.AddHeader "Content-Type", sMimeType
.AddHeader "DownloadOptions", "noopen"
.AddHeader "Content-Disposition","attachment; filename=" & sFilename
End With
c = 0
Do While c<=iSize
iChar = oStream.Read(FILE_TRANSFER_SIZE)
c = c + FILE_TRANSFER_SIZE
With Response
.BinaryWrite(iChar)
iSent = iSent + FILE_TRANSFER_SIZE
If (iSent MOD FILE_TRANSFER_SIZE) = 0 Then
.Flush
If Not .IsClientConnected Then
TransferFile = False
Exit Do
End If
End If
End With
Loop
Response.Flush
If Not Response.IsClientConnected Then TransferFile = False
'Set oStream = Nothing
End Function

%>

回复
发动态
发帖子
ASP
创建于2007-09-28

2.8w+

社区成员

ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
申请成为版主
社区公告
暂无公告