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

Discussion in 'ASP .Net' started by Ken Varn, Sep 9, 2005.

  1. Ken Varn

    Ken Varn Guest

    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
    -----------------------------------
     
    Ken Varn, Sep 9, 2005
    #1
    1. Advertisements

  2. Ken Varn

    Hans Kesting Guest

    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
     
    Hans Kesting, Sep 9, 2005
    #2
    1. Advertisements

  3. Ken Varn

    Ken Varn Guest

    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" <> wrote in message
    news:...
    > 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
    >
    >
    >
     
    Ken Varn, Sep 9, 2005
    #3
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matthew Louden
    Replies:
    4
    Views:
    3,335
    Fahad Al Hadhrami
    Nov 24, 2003
  2. =?Utf-8?B?c3RldmVfbw==?=

    Error problem: asp.net app trying to reference local file?

    =?Utf-8?B?c3RldmVfbw==?=, Feb 16, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    721
    John Oakes
    Feb 16, 2004
  3. Ryan Taylor
    Replies:
    2
    Views:
    1,325
    Ryan Taylor
    Nov 10, 2004
  4. =?Utf-8?B?QmVybmFyZCBNYXR0aGV3cw==?=

    CSV File Download from an ASP.NET page issue

    =?Utf-8?B?QmVybmFyZCBNYXR0aGV3cw==?=, Dec 17, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    11,726
    workabyte
    May 26, 2012
  5. Sam --
    Replies:
    2
    Views:
    924
    Sam --
    Mar 17, 2005
  6. news.microsoft.com
    Replies:
    0
    Views:
    392
    news.microsoft.com
    Apr 15, 2005
  7. Brett  Kelly
    Replies:
    1
    Views:
    933
    Steve C. Orr [MVP, MCSD]
    Jun 16, 2006
  8. news.microsoft.com
    Replies:
    0
    Views:
    183
    news.microsoft.com
    Apr 15, 2005
Loading...