asp.net downloading file with french character - asp.net

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

Related

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.

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);
}

"Thread is terminated" runtime error when initiating download

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);

when i try to save this file it creates and folder for the entire path

ZipFileToCreate = "c:\user\desktop\webservice\file.zip";
So, when i try to save this file it creates the folder for the path like user\desktop\webservice\file\
Why is it so?
FileStream fs = new FileStream(ZipFileToCreate, FileMode.Open);
byte[] data = new Byte[fs.Length];
BinaryReader br = new BinaryReader(fs);
br.Read(data, 0, data.Length);
br.Close();
Response.Clear();
Response.ContentType = "application/x-zip-compressed";
Response.AppendHeader("Content-Disposition", "filename=" + Parameter + ".zip");
DeleteOldFiles();
Response.BinaryWrite(data);
I think you need to be using the Environment.GetFolderPath method to find the current users' Desktop folder rather than hard-coding "c:\user\desktop\webservice\file.zip". Also using Path.Combine to build a path is more reliable than string concatenation. Try
Parameter = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "file.zip");

ASP.NET Create zip file for download: the compressed zipped folder is invalid or corrupted

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();

Resources