Response.BinaryWrite calls the ASPX page twice - c#

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

Related

Cannot redirect the page after exporting and saving the excel file

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.

CSV file download ignored in ie8/9

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.

Problem with Save As dialog in IE 7,8 in ASP.NET

i'm trying to write code that download a file that located on the server.
but the save as dialog wont open in IE.
i tried response.redirect, i tried
Response.Clear();
Response.ContentType = "text/csv";
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileNameDisplay);
Response.WriteFile(Server.MapPath(pathName + fileNameUnique));
Response.Flush();
Response.End();
every thing works on firefox and chrome , but not in internet explorer.
i know that there is a security option for this in security ---> custom level ---> Downloads ---> automatic prompting for file downloads , that is always in disable mode and i need to switch it to enable in order it to work, but i don't wont that my users deal with this.
how do i overcome this 'security problem' ?
is there any right way to deal with download files ?
what is the right code to do that ?
thank you,
gadym
var info = new FileInfo(path);
Response.Clear();
Response.AppendHeader("Content-Disposition", String.Concat("attachment; filename=", info.Name));
Response.AppendHeader("Content-Length", info.Length.ToString(System.Globalization.CultureInfo.InvariantCulture));
Response.AppendHeader("Content-Transfer-Encoding", "binary");
Response.ContentType = "text/csv";
Response.WriteFile(info.FullName, true);
Response.End();
You need explicitly tell that it is not opened content,
add following headers:
Response.AppendHeader("Content-Disposition", "attachment; filename=" + fname);
Response.AppendHeader("Content-Transfer-Encoding", "binary");
Also it is strongly recommend to set explicit file length
Response.AppendHeader("Content-Length", responseContentLength.ToString());
abatishchev, thank you for helping me.
i found a soultion to my problem.
I created a dialog window (let's call it 'DownloadWindow') which holds an empty 'A HREF' tag.
(it's shows - 'click here To Download')
after i click a download (EXCEL/CSV icon) button on my default page (which creates my file
dynamicly),
i load the dialog window ('DownloadWindow') and then i'm repleacing the 'a href' link to the file url i created earlier so my users can download it from my server.
Now, Internet explorer did pop up the open/save/cancel dialog box.
it's a bit annoying but it's solved my porblem.
I had the same problem when my link for file download was made using:
<a href="DownloadFile.aspx">
<input type="image" src="~/virtual/path/to/image.png" runat="server" />
</a>
The problem was gone when i changed it to:
<a href="DownloadFile.aspx">
<img src="~/virtual/path/to/image.png" runat="server" />
</a>

Downloading files using ASP.NET .ashx modules

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();

Link to retrieve pdf file from DB- in asp.net

I have retrieved the pdf file from Database and linked it to a "a href" tag to open pdf file using link. Now I want to give the open/save dialog box before a pdf file opened . - in javascript(by using onclick event in "a" tag to call the javascript)
You have to set the content-disposition header using C# to get this behavior in a browser.
Read this for more info
How do I prompt a "Save As" dialog for an accepted mime type?
Downloading a File with a Save As Dialog in ASP.NET
Example
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
Response.AddHeader("Content-Length", targetFile.Length.ToString);
Response.ContentType = "application/pdf";
Response.WriteFile(targetFile.FullName);

Categories

Resources