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