我有一个用经典ASP / VB脚本编写的应用程序。使用此应用程序,我允许用户在多次安全检查后下载存储在根目录之外的受保护文件夹中的文件。
|- wwwroot <- site root
| |- App <- site files
| |- ...
|- Protected
这是我的代码:
fileid = Session("DownloadFileID")
if isNumeric(fileid) Then
' This function is equal to "SELECT str_filename FROM download WHERE id_download = fileid" and returns "N/A" if nothing found
filename = GetTBLFieldValue ("download", "str_filename", "id_download", fileid, 0, 0)
' if download ID that were sent by session was correct then prompt download dialog
if filename <> "N/A" Then
Call DownloadFileForDownloadModules(filename)
Else
response.clear
response.flush
response.end
End if
End if
这是DownloadFileForDownloadModules功能:
Function DownloadFileForDownloadModules(filename)
filepath = server.mappath("../") & "/protected/"
set fso = Server.CreateObject("Scripting.FileSystemObject")
if Not fso.FileExists(filepath & filename) then
Exit Function
Else
strAbsFile = filepath & filename
Set objFile = fso.GetFile(strAbsFile)
Response.Clear
Response.AddHeader "Content-Disposition", "attachment; filename=" & objFile.Name
Response.AddHeader "Content-Length", objFile.Size
Response.ContentType = "application/octet-stream"
Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Open
'-- set as binary
objStream.Type = 1
Response.CharSet = "UTF-8"
'-- load into the stream the file
objStream.LoadFromFile(strAbsFile)
'-- send the stream in the response
Response.BinaryWrite(objStream.Read)
Response.End
objStream.Close
Set objStream = Nothing
Set objFile = Nothing
End if
End Function
问题是:
通常文件是PDF格式,但无论它们是什么,它们都是不同的文件格式,必须下载它们。
答案 0 :(得分:2)
我有一个旧应用程序也是如此,下面是我使用的标题,并且不记得有任何probs。 (它的ASP + JS,但想法是一样的)
Response.AddHeader("Expires", "0");
Response.AddHeader("Content-Transfer-Encoding", "binary");
Response.AddHeader("Content-Description", "File Transfer");
Response.AddHeader("Content-Disposition", "attachment; filename=\"report_" + (new Date()).valueOf() + sFullFilePath.substr(sFullFilePath.lastIndexOf(".")).toLowerCase() + "\";");
Response.AddHeader("Accept-Ranges", "bytes");
Response.AddHeader("Content-Length", objStream.Size);
您也可以尝试通过查看文件扩展名来添加正确的内容类型;
case ".xls":
ContentType = "application/x-msexcel";