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. Advertising

  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. Advertising

  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. Advertising

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. Ryan Taylor
    Replies:
    2
    Views:
    985
    Ryan Taylor
    Nov 10, 2004
  2. Steve C. Orr [MVP, MCSD]
    Replies:
    0
    Views:
    1,648
    Steve C. Orr [MVP, MCSD]
    Mar 7, 2005
  3. Sam --
    Replies:
    2
    Views:
    644
    Sam --
    Mar 17, 2005
  4. Brett  Kelly
    Replies:
    1
    Views:
    705
    Steve C. Orr [MVP, MCSD]
    Jun 16, 2006
  5. Replies:
    3
    Views:
    1,148
    Mark Rae
    Jan 23, 2007
Loading...

Share This Page