Double "Open/Save/Cancel/More Info" dialog when downloading text file from ASP.NET

Discussion in 'ASP .Net' started by =?Utf-8?B?dGhleWFz?=, Apr 29, 2004.

  1. How can I get my code to NOT display two "Open/Save/Cancel/More Info" dialog boxes when using the "Response.WriteFile" method to download a file to IE

    I've asked about this before and didn't get a satisfactory answer (check your browser) so now that I've had the time to set up a reasonable little test that I can post somewhere, I'll try again.

    The app I've written has three ASPX pages. One is a combined page which writes a little text file to the "LocalApplicationData" directory in the "Page_Load" method and then when you click a "Download" button, it uses the inherited Response object to write the file. The other two pages work together. The first page of that pair writes the text file and contains two "Download" buttons. One button is part of a form which uses old HTML technology to Post to the second page which does the download and the other button does a "Response.Redirect" to go to the other page to do the download. The other page has no HTML--just code in its Page_Load method to figure out the name of the file it is supposed to download and then it calls a "Download" method to use the Response object to write the file

    Essentially, the pages try three methods for doing the download. Only one works properly (the last one mentioned, above--using the Response.Redirect from one page to get to the other.) The other methods give the double-open dialog

    I've submitted a zip file to the "GotDotNet" website and got the message that it will take 72 hours for me to find out if they've accepted it. I can also email it or post it somewhere else (suggestions?)

    Oh yeah. One more thing. I thought of doing it these different ways when I noticed that some earlier VB code I'd written had worked the way I wanted. It turns out it was using the last method. So i did an experiment. I wrote the code in C# using the three methods and then did it again in VB. I get the same results with both languages (as I would have expected.

    Any ideas on getting the other methods to work properly

    The "download" part of the code looks like the following (in C#)

    protected void Download(

    string sNameOnly

    sNameOnly = sFullPath.Substring(sFullPath.LastIndexOf('\\') + 1)

    System.IO.FileInfo fi = new System.IO.FileInfo(sFullPath)
    String sFileLength = fi.Length.ToString()
    Response.ClearHeaders()
    Response.Clear()
    Response.ContentType = "text/plain"
    Response.Charset = ""
    Response.AddHeader("Content-Disposition", "attachment;filename=" + sFileName)
    Response.AddHeader("Content-Length", sFileLength)
    Response.WriteFile(sFullPath)
    Response.Flush()
    Response.Close()
    }
     
    =?Utf-8?B?dGhleWFz?=, Apr 29, 2004
    #1
    1. Advertising

  2. Hi,

    From your description, the problem you met (the file download dialog popup
    twice sometimes when using REsponse.WriteFile to write a certain file to
    the Response Stream so as to let the user download) seems to be a known
    issue of IE. And the following KB has mentioned this for IE 4.X,5.X

    #PRB: Three GET Requests Are Sent When You Retrieve Plug-in Served Content
    http://support.microsoft.com/?id=293792

    In IE6, the problem also remains , the browser will send the get request
    twice when retrieving a certain plug-in served Content.

    And from my test , when we use Response.WriteFile to write a certain
    filestream into the response and let the client download the file. The
    popup dialog appears twice when we do the "download" in the postback
    request , if we do in the first time the page is loaded(in Page_Load), it
    will only popup the dialog once. For example:

    I test using a excel file, when I try the following code, the dialog popup
    twice:
    private void Page_Load(object sender, System.EventArgs e)
    {
    if(IsPostBack)
    {
    Response.ClearHeaders();
    Response.ClearContent();
    Response.Clear();
    Response.ContentType = "application/vnd.ms-excel";
    Response.AddHeader("Content-Disposition", "attachment;filename=demo.xls");
    Response.WriteFile(Server.MapPath("./demo.xls"));
    //Response.End();
    Response.Flush();
    }
    }

    If change to this(the page first time loaded), the dialog only popup once:
    private void Page_Load(object sender, System.EventArgs e)
    {
    if(!IsPostBack)
    {
    Response.ClearHeaders();
    Response.ClearContent();
    Response.Clear();
    Response.ContentType = "application/vnd.ms-excel";
    Response.AddHeader("Content-Disposition", "attachment;filename=demo.xls");
    Response.WriteFile(Server.MapPath("./demo.xls"));
    //Response.End();
    Response.Flush();
    }
    }

    So I think this also confirm your finding that your third method which use
    Response.Redirect to forward to another page which perform the "download"
    in Page_Load will work, and other means ( in button's click event or other
    post back event) will not work properly. Currently I also haven't found any
    other means to avoid the behavior. Do you think its ok that we use the
    workaround that in button' click use Response.Redirect to forward to
    another page which is used to perform the download operation in its
    Page_load event?
    Any way, please feel free to let me know if you have any other concerns and
    thanks for posting here.

    Regards,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)

    Get Preview at ASP.NET whidbey
    http://msdn.microsoft.com/asp.net/whidbey/default.aspx
     
    Steven Cheng[MSFT], Apr 30, 2004
    #2
    1. Advertising

  3. Nope, that didn't work. (Didn't change anything.

    After looking at your post and following the link to the KB article, I tried checking the Request.Contenttype. That didn't work because the ContentType that came through wasn't "contype" as stated in the KB article, but rather "application/x-www-form-urlencoded" and also, the "Page_Load" function only ran once so adding in the "Postback" condition as you suggest made no difference. I checked my server log and realized that the request that was working properly was a "GET" and the one that wasn't was a "POST". (Contrary to what the KB article APPEARED to be saying, there was only ONE request for the page. So, I changed the form that wasn't working to a "GET" form rather than a "POST" form. THAT WORKED!!

    So, it appears that, at least in IE 6.0, this works for a "GET" but not a "POST". That would explain why a button on a page that posts back to the same page also does not work

    So, the question is: Can this work for "POST" as well as "GET"???

    Thus, my "ConfigDwnld.aspx" page generates the following (only relevant part is shown)

    <!-- This WORKS!!! --><form id="Form1" action="Download.aspx" method="get"><input type=hidden name="Filename" value="E:\temp\Test.txt"><input type="submit" value="Download" name="OldDownload"></form><!-- This does NOT WORK!!! Note: only difference is method="post"!!! --><form id="Form2" action="Download.aspx" method="post"><input type=hidden name="Filename" value="E:\temp\Test.txt"><input type="submit" value="Download" name="OldDownload"></form

    The "Download.aspx.cs" file contains the following (I'm using a text file with a .txt extension for simplicity in my testing.)

    private void Page_Load(object sender, System.EventArgs e

    if ( "contype" == Request.ContentType ) // Not necessary!!
    { // This whole block is not necessary!
    Response.ClearHeaders()
    Response.Clear()
    Response.ContentType = "text/plain"
    Response.Flush()
    Response.Close()
    }
    else

    if ( !IsPostBack ) // This condition is not necessary!
    { // The contents of this block are all that is necessary in this function
    string sFullPath = Request.Params.Get("Filename")
    string sNameOnly = sFullPath.Substring(sFullPath.LastIndexOf('\\') + 1)

    System.IO.FileInfo fi = new System.IO.FileInfo(sFullPath)
    String sFileLength = fi.Length.ToString()
    Response.ClearHeaders()
    Response.Clear()
    Response.ContentType = "text/plain"
    Response.Charset = ""
    Response.AddHeader("Content-Disposition", "attachment;filename=" + sNameOnly)
    Response.AddHeader("Content-Length", sFileLength)
    Response.WriteFile(sFullPath)
    Response.Flush()
    Response.Close()





    ----- Steven Cheng[MSFT] wrote: ----

    Hi

    From your description, the problem you met (the file download dialog popup
    twice sometimes when using REsponse.WriteFile to write a certain file to
    the Response Stream so as to let the user download) seems to be a known
    issue of IE. And the following KB has mentioned this for IE 4.X,5.

    #PRB: Three GET Requests Are Sent When You Retrieve Plug-in Served Conten
    http://support.microsoft.com/?id=29379

    In IE6, the problem also remains , the browser will send the get request
    twice when retrieving a certain plug-in served Content

    And from my test , when we use Response.WriteFile to write a certain
    filestream into the response and let the client download the file. The
    popup dialog appears twice when we do the "download" in the postback
    request , if we do in the first time the page is loaded(in Page_Load), it
    will only popup the dialog once. For example

    I test using a excel file, when I try the following code, the dialog popup
    twice
    private void Page_Load(object sender, System.EventArgs e

    if(IsPostBack

    Response.ClearHeaders()
    Response.ClearContent()
    Response.Clear();
    Response.ContentType = "application/vnd.ms-excel";
    Response.AddHeader("Content-Disposition", "attachment;filename=demo.xls");
    Response.WriteFile(Server.MapPath("./demo.xls"));
    //Response.End();
    Response.Flush();
    }
    }

    If change to this(the page first time loaded), the dialog only popup once:
    private void Page_Load(object sender, System.EventArgs e)
    {
    if(!IsPostBack)
    {
    Response.ClearHeaders();
    Response.ClearContent();
    Response.Clear();
    Response.ContentType = "application/vnd.ms-excel";
    Response.AddHeader("Content-Disposition", "attachment;filename=demo.xls");
    Response.WriteFile(Server.MapPath("./demo.xls"));
    //Response.End();
    Response.Flush();
    }
    }

    So I think this also confirm your finding that your third method which use
    Response.Redirect to forward to another page which perform the "download"
    in Page_Load will work, and other means ( in button's click event or other
    post back event) will not work properly. Currently I also haven't found any
    other means to avoid the behavior. Do you think its ok that we use the
    workaround that in button' click use Response.Redirect to forward to
    another page which is used to perform the download operation in its
    Page_load event?
    Any way, please feel free to let me know if you have any other concerns and
    thanks for posting here.

    Regards,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)

    Get Preview at ASP.NET whidbey
    http://msdn.microsoft.com/asp.net/whidbey/default.aspx
     
    =?Utf-8?B?dGhleWFz?=, May 1, 2004
    #3
  4. See the previous post for updates. You can go to GotDotNet to download a zip file that contains the original code I was working with and update from there. My previous post talked about changing from a "POST" request to a "GET" request to make things work. I still want to know how to make this work with "POST" requests

    The zip file can be downloaded from

    http://www.gotdotnet.com/Community/...ampleGuid=0cb12af2-10cf-49a3-84cb-a894da1ad38

    You can also search the "User Samples" of www.gotdotnet.com for "Download ASPX Response" and (at least when I did it, the only one I got was my submission.) The full name of the submissions is

    Download file from ASPX to the default app using Response object

    Note: I expect HIGH ratings from everyone who downloads it!! :-

    You can see the deal with "GET" and "POST" on the "ConfigDwnld.aspx" page (which you get to from the "Multi Form" button of the main page). There is a button on that page which is marked as displaying the double-open-dialog. You can make it work properly by changing the HTML code for that button so that the method is "GET" (instead of "POST"). The change is from

    <form id="Form1" action="DoDownload.aspx" method="post"

    changed to

    <form id="Form1" action="DoDownload.aspx" method="get"

    I figured this out after I submitted the code. Once I find out how to make it work for POST requests, I'll probably update the submission (if the site will allow me).
     
    =?Utf-8?B?TG93ZWxs?=, May 1, 2004
    #4
  5. Hi ,

    Regarding on the issue, I am consulting some further experts and
    finding proper resource to assist you and we will update as soon as
    posible. Thanks for your understanding.

    Regards,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security(This posting is provided "AS IS",
    with no warranties, and confers no rights.)
     
    Steven Cheng[MSFT], May 3, 2004
    #5
  6. Thanks Steven!

    Lowel

    ----- Steven Cheng[MSFT] wrote: ----

    Hi

    Regarding on the issue, I am consulting some further experts and
    finding proper resource to assist you and we will update as soon as
    posible. Thanks for your understanding

    Regards

    Steven Chen
    Microsoft Online Suppor

    Get Secure! www.microsoft.com/security(This posting is provided "AS IS",
    with no warranties, and confers no rights.
     
    =?Utf-8?B?TG93ZWxs?=, May 3, 2004
    #6
  7. Hi Lowell,

    After further consulting, Unfortunately, I've been confirmed that this is
    a known issue of IE and won't be fixed currently. It will be fixed in the
    longhorn version and is reported as the following bug.

    ============================================
    IE OE Sustained Eng. 26790 - Prompt User with Two Open or Save Dialog Boxes
    when submit with POST.
    ============================================

    Since currently we haven't anymore things on this issue, I'm sorry for the
    inconvenience it brings you. Thanks for your understanding.

    Regards,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)

    Get Preview at ASP.NET whidbey
    http://msdn.microsoft.com/asp.net/whidbey/default.aspx
     
    Steven Cheng[MSFT], May 4, 2004
    #7
  8. Steven

    Thanks for checking. I guess I'll just have to make sure I use GET requests for this purpose until I know my users are all upgraded to Longhorn

    Lowel

    ----- Steven Cheng[MSFT] wrote: ----

    Hi Lowell

    After further consulting, Unfortunately, I've been confirmed that this is
    a known issue of IE and won't be fixed currently. It will be fixed in the
    longhorn version and is reported as the following bug

    ===========================================
    IE OE Sustained Eng. 26790 - Prompt User with Two Open or Save Dialog Boxes
    when submit with POST.
    ===========================================

    Since currently we haven't anymore things on this issue, I'm sorry for the
    inconvenience it brings you. Thanks for your understanding

    Regards

    Steven Chen
    Microsoft Online Suppor

    Get Secure! www.microsoft.com/securit
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.

    Get Preview at ASP.NET whidbe
    http://msdn.microsoft.com/asp.net/whidbey/default.asp
     
    =?Utf-8?B?TG93ZWxs?=, May 5, 2004
    #8
  9. =?Utf-8?B?dGhleWFz?=

    jilinjeevan

    Joined:
    Jan 29, 2008
    Messages:
    1
    Show Save dialog when user select Save

    Hi ,
    I have created the code to download the file in my local system.
    However it shows the Open, Save, Cancel dialog.
    But if i click the 'Save' option it does not showing the Save As file dialog.
    It simply disappears. Then how can i save the file .?
    Please some one help me.

    I used the following code.

    Dim filepath As String = sName

    ' Create New instance of FileInfo class to get the properties of the file being downloaded
    Dim file As New IO.FileInfo(filepath)

    ' Checking if file exists
    If file.Exists Then

    ' Clear the content of the response
    Response.ClearHeaders()
    Response.Clear()
    Response.ClearContent()
    Response.ContentType = "text/xml"
    Response.Charset = ""

    Response.AddHeader("Content-Disposition", "attachment; filename=" & file.Name)

    ' Add the file size into the response header
    Response.AddHeader("Content-Length", file.Length.ToString())

    ' Set the ContentType
    ' Write the file into the response
    Response.WriteFile(file.FullName)
    ' End the response
    Response.Flush()
    Response.Close()

    End If
     
    jilinjeevan, Jan 29, 2008
    #9
    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. Keith
    Replies:
    1
    Views:
    431
    Carl Prothman [MVP]
    Dec 24, 2003
  2. [MSFT]
    Replies:
    0
    Views:
    379
    [MSFT]
    Apr 1, 2004
  3. Sam Thao
    Replies:
    0
    Views:
    414
    Sam Thao
    Sep 2, 2004
  4. Sydex
    Replies:
    12
    Views:
    6,659
    Victor Bazarov
    Feb 17, 2005
  5. arc
    Replies:
    0
    Views:
    124
Loading...

Share This Page