Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Problem trying to download a file from ASP.NET page.

Reply
Thread Tools

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

 
 
Ken Varn
Guest
Posts: n/a
 
      09-09-2005
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
-----------------------------------


 
Reply With Quote
 
 
 
 
Hans Kesting
Guest
Posts: n/a
 
      09-09-2005
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



 
Reply With Quote
 
 
 
 
Ken Varn
Guest
Posts: n/a
 
      09-09-2005
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Forcing Download of File Appends HTML from Download Page to Downloaded File? Brett Kelly ASP .Net 1 06-16-2006 04:05 AM
problem with code download file - two window file download Sam -- ASP .Net 2 03-17-2005 04:20 PM
File upload/download from database. Download appends aspx page to end of file Ryan Taylor ASP .Net 2 11-10-2004 08:31 PM
Help identify file trying to download to my system Arawak Computer Support 2 09-29-2004 02:25 PM



Advertisments