"Thread is terminated" runtime error when initiating download - asp.net

The situation:
In a C# web site project I am getting data out of a database and write the required data to an excel file server side, which I then want to offer for downloading.
The problem:
At the end of the code to initiate a download (See below) I get a runtime error that the thread is terminated and no file is offered for downloading.
My code
FileStream fStream = new FileStream(resultFile, FileMode.Open, FileAccess.Read);
byte[] byteBuffer = new byte[(int)fStream.Length];
fStream.Read(byteBuffer, 0, (int)fStream.Length);
fStream.Close();
response.Clear();
response.ContentType = "application/vnd.ms-excel";
response.AddHeader("Content-Length", byteBuffer.Length.ToString());
response.AddHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(resultFile));
response.TransmitFile(resultFile);
response.End();
I hope somebody can help me with this. Thanks in advance :)

I used following code to download Excel
FileStream fs = File.OpenRead(path);
byte[] data = new byte[fs.Length];
fs.Read(data, 0, (int)fs.Length);
Response.Buffer = true;
Response.ContentType = "application/x-msdownload";
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName );
Response.BinaryWrite(data);

Related

Why the TransmitFile is not downlaoding the file?

I am downloading a file from folder but this code of me doesn't download. It doesn't throw any error but doesn't download either.
Dim req As WebClient = New WebClient()
Dim response As HttpResponse = HttpContext.Current.Response
Dim filePath As String = "~/Downloads/MyExcelFile.xls"
response.Clear()
response.ClearContent()
response.ClearHeaders()
response.Buffer = True
response.AddHeader("Content-Disposition", "attachment;filename=Filename.extension")
'Dim data As Byte() = req.DownloadData(Server.MapPath(filePath))
'response.BinaryWrite(data)
response.TransmitFile(Server.MapPath(filePath))
'response.End()
try
response.WriteFile("some file");
response.Flush();
response.Close();
also consider Andrew's response on handler type - ashx is usually cleaner, with webform you might have other things that happen during page life cycle.

change aspx extension for pdf extension in Response.ContentType = "application/pdf"

I have a code that writes a pdf file into an aspx page, but the problem is that when I want to save this file it saves with aspx extension... for example:
It saves as myPDFfile.aspx and I want to save it as myPDFfile.pdf
and I have to change the extension to .pdf to be able to open it.
How can I change the extension programatically?
My code:
Dim pdfPath As String = path + Session("factura").ToString.Trim + ".pdf"
Dim client As New WebClient()
Dim buffer As [Byte]() = client.DownloadData(pdfPath)
Response.ContentType = "application/pdf"
Response.AddHeader("content-length", buffer.Length.ToString())
Response.BinaryWrite(buffer)
You should add the Content-Disposition header :
Dim pdfPath As String = path + Session("factura").ToString.Trim + ".pdf"
Dim client As New WebClient()
Dim buffer As [Byte]() = client.DownloadData(pdfPath)
Response.ContentType = "application/pdf"
Response.AddHeader("content-length", buffer.Length.ToString())
Response.AddHeader("content-disposition", "attachment; filename=myPDFfile.pdf")
Response.BinaryWrite(buffer)
You should also read this question and its answer as it will leads you to potential issue with filename characters.
You need to add a content-disposition header with the file name.
Response.AddHeader("content-disposition", "attachment; filename=myPDFfile.pdf");
This is part of RFC 2616, section 19.

Docx.dll SaveAs method throws exception

we have used codeplex for saving docx on a network path.
But intermittently the SaveAs(string path) function of this dll fails.
the path provided to the function is as expected.
Thanks in advance.
This is the code I use to create and download docx document:
DocX g_document;
g_document = WhitePageReport(dt, dtSub);
MemoryStream ms = new MemoryStream();
g_document.SaveAs(ms);
Response.Clear();
Response.AddHeader("content-disposition", "attachment; filename=\"WhitePageReport.doc\"");
Response.ContentType = "application/msword";
ms.WriteTo(Response.OutputStream);
Response.End();

How to write the context of a FileStream to my ASP.net HTTP Response?

Suppose that I want my ASP.net web server to open a file and then send it to the browser.
First, I write this:
FileInfo file = new System.IO.FileInfo(#"\\myshare\myfile.zip");
FileStream fileStream = file.Open(FileMode.Open, FileAccess.Read, FileShare.Read);
What comes next?
I would think something along the lines of Response.Write(..., but I'm having trouble figuring it out.
straight from MSDN:
HttpResponse.BinaryWrite Method
FileStream MyFileStream;
long FileSize;
MyFileStream = new FileStream("sometext.txt", FileMode.Open);
FileSize = MyFileStream.Length;
byte[] Buffer = new byte[(int)FileSize];
MyFileStream.Read(Buffer, 0, (int)FileSize);
MyFileStream.Close();
Response.Write("<b>File Contents: </b>");
Response.BinaryWrite(Buffer);
Edit: of course there are also many other methods, like streaming which allows you to never allocate the byte[] buffer all at once on the web server. This was just a starting point...
A good way to send files is using the content-disposition header before you send the actual raw data
for instance:
Response.ContentType = "application/jpeg";
Response.AddHeader("content-disposition", "attachment; filename=" + fileName);
Response.WriteFile(path);
Response.End();
where fileName is just the filename (with extension)
and path is the full path to the file

asp.net downloading file with french character

I need to download a file with french file name for example "mé.txt"..I have this code:
FileStream fileStream = File.Open("filePath", FileMode.Open);
byte[] bytContent = new byte[(int)fileStream.Length];
fileStream.Read(bytContent, 0, (int)fileStream.Length);
fileStream.Close();
string fileName = "mé.txt";
Response.AddHeader("Content-disposition", "attachment; filename=" + fileName);
Response.ContentType = "application/octet-stream";
Response.ContentEncoding = Encoding.GetEncoding("ISO-8859-1");
Response.BinaryWrite(bytContent);
But the problem is that when I have the pop up window to save my file .. Im getting this name : mé.txt
How can i fix it?
i think the problem has to be solved on the serverside using HttpUtility.UrlPathEncode

Resources