473,569 Members | 3,035 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_ForceDownl oadFile(strFile Name, strFileSaveName )
Response.Buffer = True
Dim strFileSize
Const adTypeBinary = 1
Response.Clear

'#Code Find File Size
dim fso, fileObject
Set fso = Server.CreateOb ject("Scripting .FileSystemObje ct")
Set fileObject = fso.GetFile(Ser ver.MapPath(str FileName))
strFileSize = fileObject.Size
Set fileObject = Nothing
Set fso = Nothing

dim objStream, strFileType, ContentType
Set objStream = Server.CreateOb ject("ADODB.Str eam")
objStream.Open
objStream.Type = adTypeBinary
objStream.LoadF romFile Server.MapPath( strFileName)

strFileType = lcase(Right(str FileName, 4))

Select Case strFileType
Case ".asf"
ContentType = "video/x-ms-asf"
Case ".avi"
ContentType = "video/avi"
Case ".doc"
ContentType = "applicatio n/msword"
Case ".zip"
ContentType = "applicatio n/zip"
Case ".xls"
ContentType = "applicatio n/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 = "applicatio n/rtf"
Case ".pdf"
ContentType = "applicatio n/pdf"
Case ".htm", "html"
ContentType = "text/html"
Case ".asp"
ContentType = "text/asp"
Case ".txt"
ContentType = "text/plain"
Case Else
ContentType = "applicatio n/octet-stream"
End Select

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

Response.Conten tType = ContentType
Response.Charse t = "UTF-8"
Response.Binary Write objStream.Read
Response.Flush
objStream.Close
Set objStream = Nothing
response.end
end function
Jul 22 '05 #1
0 1917

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

Similar topics

5
20686
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 browser. I think I need to do something like: $fp = fopen($filetooutput, "rb"); $thefile = fread($fp, filesize($thefiletooutput));...
2
2918
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 some file types, I get the following error messsage: IE can not download <file> from <server>. IE was not able to open this internet site. The requested...
27
5054
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 would just as soon place the employer in control of what directory he wishes to save it in, and there are two salient reasons for this: 1. I want...
8
2977
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) Response.ContentType = "application/x-unknown" ' arbitrary fn = "wallpaper_1_1.jpg" Response.write "<br>" & Server.MapPath("wallpaper") & "<br>" FPath =...
5
8473
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 to record the "valid" click -- when Open/Save was clicked. Thanks ahead. Quinn
1
7925
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 asking if the user wants to open the file after the file finished downloading?
1
1506
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 is that using it with Windows 2000, when the "Save as..." dialog box appears and a user clicks "Open" button, the dialog reappears and for opening...
2
10885
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 page</a> The above script works fine in IE. Can anyone please let me know how do I make the SaveAs dialog box open in Firefox browser. Thanks,...
0
1608
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 I have to add the Content-Disposition... So I created an other script call /download.cgi /and I obtain a popup "save as" but how the script...
0
7703
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7619
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7930
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7681
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6290
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5228
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1229
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.