By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,659 Members | 1,945 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,659 IT Pros & Developers. It's quick & easy.

Force Download fails when I select "Open" but works fine when I select "Save"

P: n/a
I have a problem forcing files to download. If I select Save the document is
saved with no problems. If I select "Open" the document is empty or I get a
"File not found" error from the application opening the file...

Code:

function UTIL_ForceDownloadFile(strFileName, strFileSaveName)
Response.Buffer = True
Dim strFileSize
Const adTypeBinary = 1
Response.Clear

'#Code Find File Size
dim fso, fileObject
Set fso = Server.CreateObject("Scripting.FileSystemObject")
Set fileObject = fso.GetFile(Server.MapPath(strFileName))
strFileSize = fileObject.Size
Set fileObject = Nothing
Set fso = Nothing

dim objStream, strFileType, ContentType
Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Open
objStream.Type = adTypeBinary
objStream.LoadFromFile Server.MapPath(strFileName)

strFileType = lcase(Right(strFileName, 4))

Select Case strFileType
Case ".asf"
ContentType = "video/x-ms-asf"
Case ".avi"
ContentType = "video/avi"
Case ".doc"
ContentType = "application/msword"
Case ".zip"
ContentType = "application/zip"
Case ".xls"
ContentType = "application/vnd.ms-excel"
Case ".gif"
ContentType = "image/gif"
Case ".jpg", "jpeg"
ContentType = "image/jpeg"
Case ".wav"
ContentType = "audio/wav"
Case ".mp3"
ContentType = "audio/mpeg3"
Case ".mpg", "mpeg"
ContentType = "video/mpeg"
Case ".rtf"
ContentType = "application/rtf"
Case ".pdf"
ContentType = "application/pdf"
Case ".htm", "html"
ContentType = "text/html"
Case ".asp"
ContentType = "text/asp"
Case ".txt"
ContentType = "text/plain"
Case Else
ContentType = "application/octet-stream"
End Select

Response.AddHeader "Content-Disposition", "attachment; filename=" &
strFileSaveName
Response.AddHeader "Content-Length", strFileSize

Response.ContentType = ContentType
Response.Charset = "UTF-8"
Response.BinaryWrite objStream.Read
Response.Flush
objStream.Close
Set objStream = Nothing
response.end
end function
Jul 22 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.