Download link of the generated file changed when I moved the code to a new pop-up page [modified]
-
Hi, this will be a bit tricky to explain but I'll try my best. Original page: I have a page with a button, which when clicked will generate a spreadsheet and prompt the user to download this spreadsheet. Changes: Now since we didn't want to the page to refresh, and at the same time we didn't want to implement an UpdatePanel to this page, we changed this button to a link which pops-up a new page. New page: The new page simply contains this button and a javascript link to close the window. I did not change the source code of the generate-spreadsheet method. Generate spreadsheet method:
public static void Export(Page webPage)
{
webPage.Response.Clear();
webPage.Response.Buffer = true;
webPage.Response.Charset = "";
webPage.EnableViewState = false;string filename = "spreadsheet.xls"; webPage.Response.ContentType = "application/vnd.ms-excel"; webPage.Response.ContentEncoding = Encoding.Default; webPage.Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName + ";"); string filePath = string.Format("{0}temp\\\\{1}", webPage.Request.PhysicalApplicationPath, fileName); populate(filePath); webPage.Response.TransmitFile(filePath); webPage.Response.Flush(); File.Delete(filePath); webPage.Response.End();
}
Problem: Now here's my problem, the code works perfectly on my local machine, but does not work when I deployed on the server. On my local, it correctly gives me a download dialog with "spreadsheet.xls" displayed. On the server, it gives me a download dialog, but the URL (http://www.oursite.com/WebForms/WFrmExport.aspx) of the pop-up page is displayed instead. So when I click "Save", it will just give me an error. Additional notes: 1. The same export method is used in the original page and the new pop-up page 2. Both original pages and pop-up pages are webforms (*.aspx) 3. The original page works on the server... meaning it correctly prompts me to download "spreadsheet.xls". The problem only occurs on the pop-up page and only on the server. 4. If I remove the
File.Delete(filePath);
statement, I will see the generated spreadsheet.xls in the correctfilepath
server directory, which is the same directory used by the original page. What could be the problem?Rafferty
modified on Friday, February 26, 2010 12:40 AM
-
Hi, this will be a bit tricky to explain but I'll try my best. Original page: I have a page with a button, which when clicked will generate a spreadsheet and prompt the user to download this spreadsheet. Changes: Now since we didn't want to the page to refresh, and at the same time we didn't want to implement an UpdatePanel to this page, we changed this button to a link which pops-up a new page. New page: The new page simply contains this button and a javascript link to close the window. I did not change the source code of the generate-spreadsheet method. Generate spreadsheet method:
public static void Export(Page webPage)
{
webPage.Response.Clear();
webPage.Response.Buffer = true;
webPage.Response.Charset = "";
webPage.EnableViewState = false;string filename = "spreadsheet.xls"; webPage.Response.ContentType = "application/vnd.ms-excel"; webPage.Response.ContentEncoding = Encoding.Default; webPage.Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName + ";"); string filePath = string.Format("{0}temp\\\\{1}", webPage.Request.PhysicalApplicationPath, fileName); populate(filePath); webPage.Response.TransmitFile(filePath); webPage.Response.Flush(); File.Delete(filePath); webPage.Response.End();
}
Problem: Now here's my problem, the code works perfectly on my local machine, but does not work when I deployed on the server. On my local, it correctly gives me a download dialog with "spreadsheet.xls" displayed. On the server, it gives me a download dialog, but the URL (http://www.oursite.com/WebForms/WFrmExport.aspx) of the pop-up page is displayed instead. So when I click "Save", it will just give me an error. Additional notes: 1. The same export method is used in the original page and the new pop-up page 2. Both original pages and pop-up pages are webforms (*.aspx) 3. The original page works on the server... meaning it correctly prompts me to download "spreadsheet.xls". The problem only occurs on the pop-up page and only on the server. 4. If I remove the
File.Delete(filePath);
statement, I will see the generated spreadsheet.xls in the correctfilepath
server directory, which is the same directory used by the original page. What could be the problem?Rafferty
modified on Friday, February 26, 2010 12:40 AM
I this it may be the issue of reading file form the server machine so you have to set account of the person who can access the local file system form server to do this : go to IIS Manager > right click on DefaultAppPool >> Go to poperties >> Go to Identity Tab >> set security account to local system
-
I this it may be the issue of reading file form the server machine so you have to set account of the person who can access the local file system form server to do this : go to IIS Manager > right click on DefaultAppPool >> Go to poperties >> Go to Identity Tab >> set security account to local system
Hi, thanks for your reply. I don't think this is the problem because it can properly download the file if I revert back my changes in the server. The old code and the new changes both access from the same file location. this is a big headache
Rafferty
-
Hi, this will be a bit tricky to explain but I'll try my best. Original page: I have a page with a button, which when clicked will generate a spreadsheet and prompt the user to download this spreadsheet. Changes: Now since we didn't want to the page to refresh, and at the same time we didn't want to implement an UpdatePanel to this page, we changed this button to a link which pops-up a new page. New page: The new page simply contains this button and a javascript link to close the window. I did not change the source code of the generate-spreadsheet method. Generate spreadsheet method:
public static void Export(Page webPage)
{
webPage.Response.Clear();
webPage.Response.Buffer = true;
webPage.Response.Charset = "";
webPage.EnableViewState = false;string filename = "spreadsheet.xls"; webPage.Response.ContentType = "application/vnd.ms-excel"; webPage.Response.ContentEncoding = Encoding.Default; webPage.Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName + ";"); string filePath = string.Format("{0}temp\\\\{1}", webPage.Request.PhysicalApplicationPath, fileName); populate(filePath); webPage.Response.TransmitFile(filePath); webPage.Response.Flush(); File.Delete(filePath); webPage.Response.End();
}
Problem: Now here's my problem, the code works perfectly on my local machine, but does not work when I deployed on the server. On my local, it correctly gives me a download dialog with "spreadsheet.xls" displayed. On the server, it gives me a download dialog, but the URL (http://www.oursite.com/WebForms/WFrmExport.aspx) of the pop-up page is displayed instead. So when I click "Save", it will just give me an error. Additional notes: 1. The same export method is used in the original page and the new pop-up page 2. Both original pages and pop-up pages are webforms (*.aspx) 3. The original page works on the server... meaning it correctly prompts me to download "spreadsheet.xls". The problem only occurs on the pop-up page and only on the server. 4. If I remove the
File.Delete(filePath);
statement, I will see the generated spreadsheet.xls in the correctfilepath
server directory, which is the same directory used by the original page. What could be the problem?Rafferty
modified on Friday, February 26, 2010 12:40 AM
I finally found the solution! I also had to clear the headers of the response, apparently,
Response.Clear()
only clears the content. Maybe the header was somehow populated incorrectly by the new pop-up page. So the fixed method is:public static void Export(Page webPage)
{
webPage.Response.ClearHeaders(); //added
webPage.Response.Clear();
webPage.Response.Buffer = true;
webPage.Response.Charset = "";
webPage.EnableViewState = false;string filename = "spreadsheet.xls"; webPage.Response.ContentType = "application/vnd.ms-excel"; webPage.Response.ContentEncoding = Encoding.Default; webPage.Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName + ";"); string filePath = string.Format("{0}temp\\\\{1}", webPage.Request.PhysicalApplicationPath, fileName); populate(filePath); webPage.Response.TransmitFile(filePath); webPage.Response.Flush(); File.Delete(filePath); webPage.Response.End();
}
Thanks to all who wanted to help! :)
Rafferty