473,396 Members | 1,995 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,396 software developers and data experts.

Problem trying to download a file from ASP.NET page.

I have an ASP.NET page that incorporates the following code on a button
press.

private void DownloadTag_Command(object sender, CommandEventArgs e)
{
FileStream fs;
String Filename;

Filename = MapPath(e.CommandArgument as string); // Name of file
is passed in cmd arg.

fs = File.Open(Filename, FileMode.Open);

byte[] byteBuffer = new byte[fs.Length];
fs.Read(byteBuffer, 0, (int)fs.Length);
fs.Close();

Response.AddHeader("Content-disposition", "attachment; filename=" +
Path.GetFileName(Filename));

Response.ContentType = "application/octet-stream";
Response.BinaryWrite(byteBuffer);
Response.End();
}

When clicking on my download button in IE, the download will call up the
download file dialog as expected. If I click on Save on the download file
dialog the file downloads without any problem. However, if I click on open,
I get a second download file dialog box with the same file. Not sure why.
If I click on open on this second dialog, the file will download and open as
expected.

Could someone explain how to eliminate this second download file dialog?
--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.

EmailID = varnk
Domain = Diebold.com
-----------------------------------
Nov 19 '05 #1
2 1640
Ken Varn wrote:
I have an ASP.NET page that incorporates the following code on a
button press.

private void DownloadTag_Command(object sender, CommandEventArgs
e) {
FileStream fs;
String Filename;

Filename = MapPath(e.CommandArgument as string); // Name of
file is passed in cmd arg.

fs = File.Open(Filename, FileMode.Open);

byte[] byteBuffer = new byte[fs.Length];
fs.Read(byteBuffer, 0, (int)fs.Length);
fs.Close();

Response.AddHeader("Content-disposition", "attachment;
filename=" + Path.GetFileName(Filename));

Response.ContentType = "application/octet-stream";
Response.BinaryWrite(byteBuffer);
Response.End();
}

When clicking on my download button in IE, the download will call up
the download file dialog as expected. If I click on Save on the
download file dialog the file downloads without any problem.
However, if I click on open, I get a second download file dialog box
with the same file. Not sure why. If I click on open on this second
dialog, the file will download and open as expected.

Could someone explain how to eliminate this second download file
dialog?


I don't know why you get that second dialog.

Your code can be somewhat shorter, when you use the Response.WriteFile
method. You will still need the Content-Disposition, but you don't need
to read in the file yourself.

Hans Kesting

Nov 19 '05 #2
The problem still seems to occur when I use WriteFile as well.

--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.

EmailID = varnk
Domain = Diebold.com
-----------------------------------
"Hans Kesting" <ne***********@spamgourmet.com> wrote in message
news:eV**************@TK2MSFTNGP12.phx.gbl...
Ken Varn wrote:
I have an ASP.NET page that incorporates the following code on a
button press.

private void DownloadTag_Command(object sender, CommandEventArgs
e) {
FileStream fs;
String Filename;

Filename = MapPath(e.CommandArgument as string); // Name of
file is passed in cmd arg.

fs = File.Open(Filename, FileMode.Open);

byte[] byteBuffer = new byte[fs.Length];
fs.Read(byteBuffer, 0, (int)fs.Length);
fs.Close();

Response.AddHeader("Content-disposition", "attachment;
filename=" + Path.GetFileName(Filename));

Response.ContentType = "application/octet-stream";
Response.BinaryWrite(byteBuffer);
Response.End();
}

When clicking on my download button in IE, the download will call up
the download file dialog as expected. If I click on Save on the
download file dialog the file downloads without any problem.
However, if I click on open, I get a second download file dialog box
with the same file. Not sure why. If I click on open on this second
dialog, the file will download and open as expected.

Could someone explain how to eliminate this second download file
dialog?


I don't know why you get that second dialog.

Your code can be somewhat shorter, when you use the Response.WriteFile
method. You will still need the Content-Disposition, but you don't need
to read in the file yourself.

Hans Kesting

Nov 19 '05 #3

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

Similar topics

9
by: Theodore A. Jencks | last post by:
Hi All, I have a problem that is two fold. First a little about the enviroment I'm using; I am running Microsoft Windows XP with all security patches applied. I use Mozilla 1.6 for all testing...
0
by: Richard L Rosenheim | last post by:
I'm looking to display a page and then initiate the downloading of a file. Basically trying to display a page that says the download will begin shortly, and if it doesn't, click here. Like what...
7
by: Sean | last post by:
Hi I've written an HTML page that has a link to an MPG file, to be burned onto a "Demo CD". The MPG file is quite big - 500MB. On my computer, it works fine - clicking the link opens up Windows...
11
by: Russ | last post by:
My web app writes some binary data to a file at the client site via Response.Write and Response.BinaryWrite. This action is accomplished in response to a button click, with C# code behind as...
7
by: Bala | last post by:
Hi All, I am trying to download the file from network machine (under same domain). I am using below link page code to impersonate. http://www.netomatix.com/ImpersonateUser.aspx I can able...
2
by: Flurry | last post by:
Hello I've got a problem and I'm not getting much help trying to resolve it. I've searched google all morning for an answer.... I have the following code: Lab1.text = "Your Serial Number...
16
by: matt | last post by:
I have used some free code for listing files for download, but I want to send an email to the administrator when the file has been downloaded. I have got some code in here that does it, but it will...
1
by: Blackstar | last post by:
I have a website I am trying to download a file from. This website is strange in the fact that there is no direct link to the URL of the file. The way the site works is as follows: 1) Enter in...
5
by: althafexcel | last post by:
hi everyone Im trying to include an external js in my aspx page under the head tag, it doesn't load or it displays an object expected error whenver the function from the .js is called. Actually...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.