Why Does FireFox Not Include the .xml Extension when Downloading a File? - asp.net

OK. I'm sure it does download XML files with the .xml extension, but I'm wondering what is missing in the code here to cause the .xml extenstion to be missing from the downloaded file.
Note: This works in IE 6+ (didn't try WebKit based browsers or Opera)
private void GenerateXmlAttachment(string xmlInStringFormat, string fileName)
{
// Where fileName = "someFile.xml"
HttpResponse response = HttpContext.Current.Response;
response.Clear();
response.Charset = string.Empty;
response.ContentEncoding = Encoding.Default;
response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
response.AddHeader("Content-Length", xmlInStringFormat.Length.ToString());
response.ContentType = "text/xml";
response.Write(xmlInStringFormat);
response.Flush();
response.End();
}
Ideas anyone?

Try changing:
response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
To:
response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName));
The code works for all browsers (including Firefox which we use heavily).

Solved the firefox spaces problems.
Surround your filename with quotes.
Change the code below
response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
to
response.AddHeader("Content-Disposition", "attachment;filename=\"" + fileName + "\"");

Does your filename have space in it? Firefox may have problem with that.
See this blog post for more details:
http://blog.mjjames.co.uk/2009/04/content-disposition-in-different.html

Related

open pdf stream file in iframe

I want to open a PDF file in my browser using iframe or object tag or what evere can do so
I have an PDF.aspx page:
Response.Buffer = false; //transmitfile self buffers
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment; filename=file1.pdf");
Response.TransmitFile(filepath);
Response.End();
and this is my iframe in another page:
<iframe runat="server" src="PDF.aspx" class="pdfFilePanel_fn" width="99%" height="98%"></iframe>
it opens the pdf file not showing it inside the iframe!!!
Any idea or better solution?
Response.AddHeader("Content-Disposition", "attachment; filename=file1.pdf");
should be changed to
Response.AddHeader("Content-Disposition", "inline; filename=file1.pdf");
:)

Some sites do not allow requesting the same file twice

I am going to set a download for user, but when I using this code:
Response.Clear();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + FileName + ".pdf");
Response.WriteFile(Server.MapPath(#"Resumes\" + FileName + ".pdf"));
Response.End();
this error will shown:
and when I use this code:
string filename = FileName + ".pdf";
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + filename);
string aaa = Server.MapPath("~/Resumes/" + filename);
Response.TransmitFile(Server.MapPath("~/Resumes/" + filename));
Response.End();
this error will shown:
when I'm trying to download file with browser, it completely downloads without any error, but IDM shows these errors !!!
I am using asp.net 4.5 and IIS 8
I tried this code, it seems that it acts good. but I didn't saw this code anywhere in any website for file download, so I am not sure I doing right. anyway it is working!!!
Response.RedirectPermanent("http://MySite.com/Resumes/" + FileName + ".pdf");

Opening a word file after downloading from code behind(asp.net)

I have written code to open a Word document after downloading it in code behind. The document is opening fine, but it is not saving in Word format. When I am going to open it, it is asking for selecting the format to open the file.
The code is below:
string FullFilePath = "D:\\ASP\\ASP.doc";
FileInfo file = new FileInfo(FullFilePath);
if (file.Exists)
{
Response.ContentType = "application/vnd.ms-word";
Response.AddHeader("Content-Disposition", "inline; filename=\"" + txtDate.Text + "\"");
Response.AddHeader("Content-Length", file.Length.ToString());
Response.TransmitFile(file.FullName);
}
Set your content type to application/msword.
Refer: Microsoft Office MIME Types
You are not specifying an extension when sending the file name.
If your file saves without an extension, you will get the prompt asking for the application to use to open it.
Also, use the "Content-Disposition", "Attachment" if you want to tell the browser to save the file. inline will make the browser attempt to open the file in Word directly.
string FullFilePath =//path of file //"D:\\ASP\\ASP.doc";
FileInfo file = new FileInfo(FullFilePath);
if (file.Exists)
{
Response.ContentType = "application/msword";
Response.AddHeader("Content-Disposition", "Attachment; filename=\"" + txtDate.Text + ".doc\"");
Response.AddHeader("Content-Length", file.Length.ToString());
Response.TransmitFile(file.FullName);
}

ASP.Net Download file to client browser

I'm writing a simple test page to download a text file from a browser on button click. I am getting a really strange error that I have never seen before. Any thoughts?
The error occurs on Response.End(); and the file never gets to the client browser
Code:
string filePath = "C:\\test.txt";
FileInfo file = new FileInfo(filePath);
if (file.Exists)
{
Response.ClearContent();
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "text/plain";
Response.TransmitFile(file.FullName);
Response.End();
}
Error:
Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.
Try changing it to.
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "text/plain";
Response.Flush();
Response.TransmitFile(file.FullName);
Response.End();
Just a slight addition to the above solution if you are having problem with downloaded file's name...
Response.AddHeader("Content-Disposition", "attachment; filename=\"" + file.Name + "\"");
This will return the exact file name even if it contains spaces or other characters.

Force PDF Download in ASP.NEt with IE

So, I user a bit of code to force a download on my ASP.Net based project. This bit of code works in Firefox and Chrome, bu not in IE for some strange reason. Even stranger, it worked in all three initially, and just stopped working in IE recently. Below is the code I used, please let me know if any adjustments need to be made or what the problem with with may be.
string path = MapPath(fname);
string name = Path.GetFileName(path);
string ext = Path.GetExtension(path);
string type = "Application/pdf";
Response.AppendHeader("content-disposition","attachment; filename=" + path);
Response.WriteFile(path);
Response.End();
More details
Here is the revamped code, still doesnt work for IE.
string path = MapPath(fname);
string name = Path.GetFileName(path);
string ext = Path.GetExtension(path);
string type = "Application/pdf";
Response.ClearHeaders();
Response.ClearContent();
Response.ContentType = type;
Response.AddHeader("content-disposition","attachment; filename=" + path);
Response.WriteFile(path);
Response.End();
You should probably try to set the mime type to "application/octet-stream". If you don't want a specific handler to respond to the mime-type.
Should this code
Response.AddHeader("content-disposition","attachment; filename=" + path);
be changed as
Response.AddHeader("content-disposition","attachment; filename=" + name + "." + ext);
or
Response.AddHeader("content-disposition","attachment; filename=" + name + ".pdf");
Other things to check for
Response.Buffer to true in the beginning
Response.clear in the beginning
Use response.binarywrite instead of writefile
Response flush at the end
Ensure no HTML or space characters written to the response.stream other than the binarywrite.
Problem solved. The reason it was not going through was due to an extra form that was present on the master page, apparently overlaying the buttons. Once that was fixed, It worked properly in all browsers.
Adding the following two lines at the top, fixed it for me:
Response.ClearContent();
Response.ClearHeaders();

Resources