IE11 not respecting filename of content-disposition - c#

My code is opening a PDF in a tab/new window using an .apsx page and the following code:
string fileName = GetFileName(so);
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "inline; filename=" + fileName);
Response.BinaryWrite(pdfStream);
When selecting the save option in the browser for the PDF, the filename in the save dialogue is not being populated correctly in IE (11); it's still using the page name. Chrome and FF are working like I'd expect them too (using the provided filename as the suggested name).
In addition, IE works the same as Chrome and FF when i change the content-disposition to attachment instead of inline. They all use the filename as the suggested save name.
Am I missing something in how to use the inline functionality?

Did you try change inline to attachment ?
Response.AddHeader("content-disposition", "attachment; filename=" + fileName);

Related

while downloading excel template from local system the special character '#' in the template name is changing to '_' automatically

I have 1 excel template in my local system named as "CLIA#OfLabTemplate.xls"
while downloading this template from UI in .net, the template name is getting changed to "CLIA_OfLabTemplate.xls".
What should I do so that the file name will not change ?
var filepath = Server.MapPath("~/App_Data/CLIA#OfLabTemplate.xls");
Response.ClearContent();
Response.AddHeader("Content-Disposition", "attachment; filename=" + myfile.Name);
Response.AddHeader("Content-Length", myfile.Length.ToString(CultureInfo.InvariantCulture));
Response.ContentType = "xls";
Response.TransmitFile(myfile.FullName);
Response.End();
A minor comment to start with: it is not a template(.xlt), but a sheet (.xls).
The ContentType should be application/vnd.ms-excel as in this post:
https://stackoverflow.com/a/4212908/200824
Or check the next comment of that post to use System.Web.MimeMapping.GetMimeMapping(yourFileName) (.net 4.5)
https://stackoverflow.com/a/44677607/200824

Fetch word document from an API and download it

I have a 3rd party API. When i call that api it will output a word document.
In my .net website i have an ASPX page. When user browse that page, i have to call that 3rd party API, get the word document and should send it to user as a downloadable word document.
Note: I cannot call that 3rd party api using javascript for security reasons.
I tried below code
WebClient wClient = new WebClient();
var pagesource = wClient.DownloadString("3rd party api");
Response.Clear();
Response.AddHeader("Content-Type", "application/msword");
Response.AddHeader("Content-Disposition", "attachment; filename=test.docx");
Response.Write(pagesource);
Response.End();
It downloads the word document. But when i open it, it is corrupted.
I believe wClient.DownloadString is going to download the document as a string. You want to use .DownloadData to get the data as a byte array (binary). And then something like:
var fileBytes = wClient.DownloadData("3rd party api");;
if (fileBytes == null) return;
string filename = DateTime.Now.ToString("yyyy-MM-dd") + "-filename.docx";
Response.Clear();
Response.AddHeader("Content-Length", fileBytes.Length.ToString(CultureInfo.InvariantCulture));
//Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", filename)); // save file as attachment
Response.AddHeader("Content-Disposition", string.Format("inline; filename={0}", filename)); // display inline in browser
Response.AddHeader("Content-Type", "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
Response.BinaryWrite(fileBytes);
Response.Flush();
Response.End();
Pretty sure your mime type is incorrect for docx. You are using the one for .doc.
Try
Response.AddHeader("Content-Type", "application/vnd.openxmlformats-officedocument.wordprocessingml.document");

Content-Disposition Filename not working on IE

I am working on an asp.net/c# web application that allows users to view and download PDF files. When I am clicking on a file, I get to view that in the PDF reader available in the browser, and when I save it, the file should be saved with the name that I have passed via headers. But this is not the behavior in IE7 & IE8
FileInfo f = new FileInfo(FileName);
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "inline; filename=" + f.Name)
When I click to save a file, the filename that I am sending over is not being used to save the file, but the filename of the aspx page in the url is being taken. Is this is a bug in IE. Everything works fine when I try it on Google chrome.
Try Response.AddHeader("Content-Disposition", "attachment;filename=\"" + f.name + "\""); Inline is not working under IE 7-8.

Save download file dialog

I have created a PDF using iText and storing it in a particular location (specified in the code). I would like to prompt a save dialog box for the user to choose location on his computer to save the pdf. I checked iText tutorial but it didn't help me.
Here is the code for generating the PDF file:
Document objDoc = new Document();
PdfWriter.GetInstance(objDoc, new FileStream("C:\\HelloWorld.pdf", FileMode.Create));
objDoc.Open();
objDoc.Add(new Paragraph("welcome iText Pdf"));
objDoc.Close();
I tried like this for saving:
string FileName ="HelloWorld.pdf";
String FilePath = #"C:\";
HttpResponse response = HttpContext.Current.Response;
response.ClearContent();
response.Clear();
response.ContentType = "application/pdf";
response.AddHeader("Content-Disposition", "attachment; filename=" + FileName + ";");
response.TransmitFile(FilePath + FileName);
response.Flush();
response.End();
I'm assuming you're doing this from a web page since you tagged this ASP.NET. You need to add the Content-Disposition header. See the following question for details:
Force download of a file on web server - ASP .NET C#

Specifying filename for dynamic PDF in asp.net

How can I specify the filename when dumping data into the response stream?
Right now I'm doing the following:
byte[] data= GetFoo();
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/pdf";
Response.BinaryWrite(data);
Response.End();
With the code above, I get "foo.aspx.pdf" as the filename to save. I seem to remember being able to add a header to the response to specify the filename to save.
Add a content-disposition to the header:
Response.AddHeader("content-disposition", #"attachment;filename=""MyFile.pdf""");
FYI... if you use "inline" instead of "attachment" the file will open automatically in IE. Instead of prompting the user with a Open/Save dialogue.
Response.AppendHeader("content-disposition", string.Format("inline;FileName=\"{0}\"", fileName));
Response.AppendHeader("Content-Disposition", "attachment; filename=foo.pdf");
For some reason, most of the answers out there don't seem to even attempt to encode the file name value. If the file contains spaces, semicolons or quotes, it mightn't come across correctly.
It looks like you can use the ContentDisposition class to generate a correct header value:
Response.AppendHeader("Content-Disposition", new ContentDisposition
{
FileName = yourFilename
}.ToString());
You can check out the source code for ContentDisposition.ToString() to confirm that it's trying to encode it properly.
Warning: This seems to crash when the filename contains a dash (not a hyphen). I haven't bothered looking into this yet.
Response.AddHeader("Content-Disposition", "attachment;filename=" & FileName & ";")

Categories