Thursday, March 22, 2012

What is the correct encoding to download a file?

I would like to perform a file download from a dedicated ASP.NET page (the file is originally stored in a MS SQL database). Here below is the code-behind that I am using.

Yet, when I try to download a simple PDF file, the result is corrupted and cannot be opened back.

Can anyone figure out the error that I am doing within this code?

Thanks in advance,
Joannes

private void Page_Load(object sender, System.EventArgs e)
{
string filename = //snipped;
string mimetype = // snipped;
byte[] filedata = //snipped;

char[] chardata = Response.ContentEncoding.GetChars(filedata);

Response.ContentType = mimetype;
Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename);

Response.Write(chardata, 0, chardata.Length);
Response.Flush();
}what are you getting for the "mimetype" value? I'm betting that's the issue...
Nope, it wasn't the issue. The problem was that I didn't needed any encoding at all... All I had to do was to useResponse.BinaryWrite instead.

Thanks for answering btw,
Joannes

0 comments:

Post a Comment