473,414 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

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

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
0 1905

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Richie | last post by:
What I want is to have a link to a file (it could be a .zip, .exe, .jpg, ..txt or even .html) and when the user clicks on it they are prompted with the Save As box, as opposed to it opening in the...
2
by: RickL | last post by:
I have an ASP application that uploads a specified file to the server. To retrieve the file, I simply assign the filepath and file to a hyperlink on the page. When you click "Save Target As" for...
27
by: Curious Angel | last post by:
I have a resume in PDF format and I want anyone who LEFT-OR-RIGHT clicks the link to force the file to be saved, and in any event _not_ opened. Since the PDF will be in his cache in any event, I...
8
by: dave | last post by:
Hi there I'm trying to force save as dialogue using this code in download.asp..but when it runs it prompts for download.asp to save or open. code snippet (from aspfaq.com) ...
5
by: Quinn | last post by:
When users clicked a unkown mime type link such as Zip on my website, a "Save/Open/Cancel" dialog box pops up. Is there a way to detect which button users clicked by using ASP? actually I only what...
1
by: suitland | last post by:
Hi All, I am using the execCommand(SaveAs) function to save an HTML file as an xls file, and works fine when I open the program and open the file. Is there anyway I can trigger an Open dialog box...
1
by: Tomas Martinez | last post by:
Hi, In my web I have a proccess of download reports which on first step generates a PDF, then flushes the buffer (the generated file) and finally deletes this file from server. Ok, the problem...
2
by: psanjay | last post by:
Hi, Am unable to open javascript SaveAs dialog box in Firefox browser. Following is the script, <a href="javascript:void(0);" onclick="document.execCommand('SaveAs',false,'*.xyz');">Save this...
0
by: lety28 | last post by:
I have a script "licence.cgi" write in python which generate text files and I would like that the user click on a button and he can download this file. _1er solution:_ I found in my research that...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.