473,403 Members | 2,071 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

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

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()
}
Nov 18 '05 #1
7 3675
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


Nov 18 '05 #2
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



Nov 18 '05 #3
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/U...cb-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).
Nov 18 '05 #4
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.)

Nov 18 '05 #5
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.
Nov 18 '05 #6
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

Nov 18 '05 #7
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


Nov 18 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Morten Overgaard | last post by:
Hi all Is it possible for a .Net app. to intercept the "open" action on files in a given directory. I want to intercept the "open" action and the perhaps direct the client ( the app. which is...
5
by: Quinn | last post by:
When users clicked a unkown mime type link such as Zip on my website, a "Save/Open/Cancel" dialog box pops up. Is there a way to detect which button users clicked by using ASP? actually I only what...
1
by: Keith | last post by:
I have a minor problem with a piece of code I have written to download a CSV file in an ASP.NET application. It works fine except that if the user presses the Open button on the IE6 download...
3
by: Rico | last post by:
Hello, Is there any kind of Browse dialog available? I currently use an Open File dialog which is great for selecting files, but sucks (it's confusing) when you're trying to set a directory...
2
by: OutdoorGuy | last post by:
Greetings, I have a "newbie" question in relation to opening files from C#. I have a Windows form where I allow the user to type in a file extension in a text box (e.g., "xls"). I then take...
4
by: Wayne | last post by:
How do I get rid of the generic Windows "Open File - Security Warning" that appears when I try to open a database that resides on another PC on my home network? This is not the annoying macro...
0
by: Ofelia | last post by:
Hi, I'm new to this forum and to Perl language but I would like to ask for your help. I'm working in Linux and the files I need to process are in the format “file.gz”. I created a script which...
3
by: anudu | last post by:
hi, I am developing a system with asp.net, c#, and ajax. I have an excel file in server in "Server.MapPath("ExcelFiles/Test.xls")". I want to make it available to save to the disk in client...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.