I have some code in a button click event which gets a csv string from a hidden input and writes it to the response as a CSV file.
This work fine in Chrome, Firefox, ie7, ie9 in quirks mode. However it does not work in ie8 or ie9 default.
Looking at this in fiddler the csv is being written to the response but the another get request is being made immediately after and the page reloads. No file saving dialog appears.
protected void btnCsvHidden_Click(object sender, EventArgs e)
{
var csv = csvString.Value;
var filename = "Reporting";
Response.Clear();
Response.ClearHeaders();
Response.AddHeader("Cache-Control", "no-store, no-cache");
Response.AddHeader("content-disposition", "attachment; filename=\"" + filename + ".csv\"");
Response.ContentType = "text/csv";
Response.Write(csv);
Response.End();
}
The problem was with my own IE, I ran a load of windows updates and now it works so I'm not sure what it was exactly.
Related
I have the same code to create an Excel file and download it on several pages.
The code is working fine.
byte[] report = //get report as bytes
Response.AddHeader("content-disposition", String.Format("attachment;filename={0}", "Filename.xlsx"));
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.ms-excel";
Response.BinaryWrite(report);
Response.End();
I try to add it to another page, but it does not work. It gets stuck on the last line of code:
Response.End();
It isn't related to the report content, I tried to get the same report data on two pages and it worked on one and failed on the other.
It appears to be something in the page settings, but what?
I found the solution!
I added a trigger to the button that executes the code and the issue is resolved.
The code looks like this:
In aspx:
<asp:Button ID="btnExportToExcel" runat="server" Text="Export to Excel" OnClick="btnExportToExcel_Click" />
<asp:PostBackTrigger ControlID="btnExportToExcel" />
In the code behind:
protected void btnBudgetExportToExcel_Click(object sender, EventArgs e)
{
byte[] report = //get report as bytes
Response.AddHeader("content-disposition", String.Format("attachment;filename={0}", "Filename.xlsx"));
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.ms-excel";
Response.BinaryWrite(report);
Response.End();
}
There is my problem. I'm trying to download file from asp.net project directory. File is in "Date" directory. I have done WebForm1.aspx page with GridView that display files in Date directory. All files is displaying like a link.
There is my code bottom.
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if(e.CommandName == "Download")
{
Response.Clear();
Response.ContentType = "text/plain";
Response.AppendHeader("content-disposition", "filename" + e.CommandArgument);
Response.TransmitFile(Server.MapPath("~/Data/") + e.CommandArgument);
Response.End();
}
}
I go through the link with file. After that my browser offer to download not my file but the file of all page (WebForm1.aspx)
Help me solve this problem. What have I done incorrect?
image with problem
Your disposition header is wonky (no attachment, missing =), change to:
Response.AppendHeader("content-disposition", "attachment; filename=" + e.CommandArgument);
ExportToCSVFile(DtExcel);
Response.ContentType = "Application/x-msexcel";
Response.AddHeader("content-disposition", "attachment;filename=" + GetCustomerUserID() + "_UserList.csv");
Response.Write(ExportToCSVFile(DtExcel));
// Context.ApplicationInstance.CompleteRequest();
Response.Redirect("Redirect_Url_ExportToExcel.aspx", false);
// Context.ApplicationInstance.CompleteRequest();
// Response.End();
hi
I am exporting and saving the excel file. After saving the file the page is still doing some processing. To abort that processing i need to redirect the page. If I' redirect the page the SaveFileDilaog doesn't appear. If it appears at that time the page does not redirect. Please give some solution.
I am trying to open a file in the browser using BinaryWriter. This causes the a dialog window to open and prompt the user to save or open the file. That behavior is fine; however, if I select open, the aspx page is called again and after a long wait the file finally opens.
I set the ContentType
Response.BinaryWrite(binary);
Response.End();
Repsonse.Close();
This behavior only occurs with excel and word files.
Browser IE8
A substitute way of doing the same. You can check it out:
FileInfo fileInfo = new FileInfo(yourFilePath);
context.Response.Clear();
context.Response.ContentType = contentType; //Set the contentType
context.Response.AddHeader("content-disposition", "attachment; filename=" + Path.GetFilename(yourFilePath));
context.Response.AddHeader("Content-Length", fileInfo.Length.ToString());
context.Response.TransmitFile(yourFilePath);
Hope this will help
I have ASP.NET page with an iframe on it for displaying some pdf reports on this page.
When user select the report type from dropdown, I add the needed for report data into the ASP.NET Session and change the attribute "src" of the iframe to .ashx module address which generates the .pdf report.
But if Adobe glug-in for viewing .pdf files in browser is not installed, browser proposes to save report file and name of the proposed file is "HandlerName.ashx".
But I want to make the browser proposed to save the file with the name "Report.pdf". Can I do this? Are there any workarounds?
Add the following header in your ashx-file:
context.Response.AddHeader("content-disposition", "attachment;filename=PUTYOURFILENAMEHERE.pdf");
context.Response.ContentType = "application/pdf";
Try inline content-disposition, however I have to check this:
Response.ContentType = "application/pdf";
Response.AppendHeader("content-disposition", "inline; filename=" + name );
Use
Response.Clear();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=Report.PDF");
Response.WriteFile(Server.MapPath("~/YourPath/Report.PDF"));
Response.End();