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

K

Ken Varn

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
-----------------------------------
 
H

Hans Kesting

Ken said:
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
 
K

Ken Varn

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 said:
Ken said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top