Network Failure when using Response.TransmitFile - c#

I'm trying to use below code, to allow the user to download an excel that is located on a local server:
FileInfo dest_file= new FileInfo(filename);
Response.ClearContent();
Response.ContentType = "Excel/xls";
Response.AddHeader("Content-Disposition", string.Format("attachment; filename = {0}", filename));
Response.AddHeader("Content-Length", dest_file.Length.toString());
Response.TransmitFile(filename);
Response.End();
However, after publishing the solution, downloading the file results in a Network failure. Any idea how I can solve this?
Note that the file downloads successfully when trying to access it through directory browsing.

it seems your using an invalid content-type
for xlsx use this
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
for xls
Response.ContentType = "application/vnd.ms-excel"

Related

Open pdf file from server location in c#

We have a requirement where I need to open the pdf file from particular location in server i.e. "C:\PdfFile\Test.pdf".
I have tried this solution:
string fileName = lnk.CommandArgument.ToString();
System.Diagnostics.ProcessStartInfo a = new System.Diagnostics.ProcessStartInfo(fileName, "Open");
System.Diagnostics.Process.Start(a);
This is working for local as we have same path in our local But this is not working when we host the site.
In you ASP.NET form application you have to add this code:
Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition", "attachment; filename=MyFile.pdf");
Response.TransmitFile(Server.MapPath(#"C:\PdfFile\Test.pdf"));
Response.End();

How to manage generated files, how long to keep them using asp.net

I have a web application that generates excel files from a template.
When the new file is streamed to the client I deleted the file. This works fine in my computer, but for whatever reason, when accesing the website from another computer, the file is deleted before streamed to the client, so the file is always empty (0 bytes)
Here is the code:
try
{
Response.Clear();
Response.ClearHeaders();
Response.ContentType = "application/ms-excel";
Response.AddHeader("content-disposition", "attachment;filename=" + newFN + ".xlsx");
Response.TransmitFile(DestFile);
Response.Flush();
Response.End();
}
finally
{
File.Delete(DestFile);
}
Why this works fine from my computer, and it doesn't from another computer?
What can I do to delete generated files and when? I'm thinking in having like a schedule task, to check by date, all files before specific date, and delete them, but is there any other solution?
The following works for me:
Response.Clear();
Response.ClearHeaders();
Response.ContentType = "application/ms-excel";
Response.AddHeader("content-disposition", "attachment;filename=" + newFN + ".xlsx");
Response.TransmitFile(DestFile);
Response.Flush();
File.Delete(DestFile);
HttpContext.Current.ApplicationInstance.CompleteRequest();
But you should consider use the Response.BinaryWrite method if the file is being generated dinamically.

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#

ZIP download contains html content

When I use the following code to download a ZIP file it appears to work. However, when I attempt to open the downloaded ZIP, I get an 'invalid compressed folder' message. When I open the ZIP in notepad I see it is filled with HTML.
string fp = Server.MapPath("directory\\file.zip");
FileInfo file = new FileInfo(fp);
if (file.Exists)
{
Response.ClearContent();
Response.AddHeader("content-disposition","attachment; filename=" + file.Name);
Response.AddHeader("content-length", file.Length.ToString());
Response.ContentType = "application/zip";
Response.TransmitFile(file.FullName);
Response.End();
}
An issue I can't seem to fix that is probably related is when I try to manually type in the address of the file (http://website.com/downloads/file.zip), I get a redirect (http://website.com/login.aspx) even when logged in as the admin. Any pointers in where to look would be greatly appreciated.
Instead of just using Response.ClearContent() also use Response.ClearHeaders() to remove all the current headers as well as the body of the response.
From MSDN, HttpResponse.ClearContent Method:
The ClearContent method does not clear header information.

Response.WriteFile PDF files - corrupted files

I am having problem with writing PDF files to browser. Other mime types work fine.
PDF files become corrupted.
FileInfo file = new FileInfo(Path.Combine(_module.FileDir, _file.FilePath));
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = _file.ContentType;
Response.AppendHeader("Content-Disposition", "attachment; filename=" + Regex.Replace(_file.FilePath, "\\s", "-"));
Response.AppendHeader("Content-Length", file.Length.ToString());
try
{
Response.WriteFile(file.FullName);
Response.Flush();
Response.Close();
}
catch
{
Response.ClearContent();
}
My problem was with HTTP Module. I was applying White space filter
HttpApplication app = sender as HttpApplication;
if (app != null && app.Request.RawUrl.Contains(".aspx"))
{
app.Response.Filter = new WhitespaceFilter(app.Response.Filter);
}
IIS HTTP Compression and Streaming PDF's: Don't work well.
http://blog.1530technologies.com/2006/11/iis_http_compre.html
You need these three statements:
Response.Flush();
Response.Close();
Response.End();
The last one is the most important.
For this situation, a Response.Redirect should work just as well:
FileInfo file = new FileInfo(Path.Combine(_module.FileDir, _file.FilePath));
Response.Redirect(file.FullName);
Are you sure you're getting the right MIME type?
Are you attempting to force the user to download, or just stream out the PDF data?
Are you performing a Response.End() call anywhere to ensure that no extra data (outside of the headers and the PDF binary) is sent?
I'm thinking it's #3 that may be your issue here. Microsoft's Knowledge Base provides this code to do, essentially, what you seem to be doing.
//Set the appropriate ContentType.
Response.ContentType = "Application/pdf";
//Get the physical path to the file.
string FilePath = MapPath("acrobat.pdf");
//Write the file directly to the HTTP content output stream.
Response.WriteFile(FilePath);
Response.End();

Categories