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");
Related
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);
}
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.
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();
string fileName = "test.zip";
string path = "c:\\temp\\";
string fullPath = path + fileName;
FileInfo file = new FileInfo(fullPath);
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.Buffer = true;
Response.AppendHeader("content-disposition", "attachment; filename=" + fileName);
Response.AppendHeader("content-length", file.Length.ToString());
Response.ContentType = "application/x-compressed";
Response.TransmitFile(fullPath);
Response.Flush();
Response.End();
The actual zip file c:\temp\test.zip is good, valid, whatever you want to call it. When I navigate to the directory c:\temp\ and double-click on the test.zip file; it opens right up.
My problem seems only to be with the download. The code above executes without any issue. A file download dialog is presented. I can chose to either save or open. If I try to open the file from the dialog, or save it and then open it. I get the following dialog message:
The Compressed (zipped) Folder is invalid or corrupted.
For Response.ContentType I've tried:
application/x-compressed
application/x-zip-compressed
application/x-gzip-compresse
application/octet-stream
application/zip
The zip file is being created with some prior code (that I'm sure is working fine due to my ability to open the created file directly) using: Ionic.zip
http://www.codeplex.com/DotNetZip
This worked. I don't know why but it did.
string fileName = "test.zip";
string path = "c:\\temp\\";
string fullPath = path + fileName;
FileInfo file = new FileInfo(fullPath);
Response.Clear();
//Response.ClearContent();
//Response.ClearHeaders();
//Response.Buffer = true;
Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
//Response.AppendHeader("Content-Cength", file.Length.ToString());
Response.ContentType = "application/x-zip-compressed";
Response.WriteFile(fullPath);
//Response.Flush();
Response.End();
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