C
Cathryn Johns
Hi
I'm trying to download some content as a file to the client. My code
behind looks like this:
private void DownloadFile(byte[] contents)
{
Response.ContentType = "text/csv";
Response.AppendHeader("content-disposition",
"attachment;filename=myFile.csv");
Response.AppendHeader("content-length",contents.Length.ToString());
Response.BinaryWrite(contents);
}
It works to an extent, in that it pops up the file download dialog and
writes a file to the user's selected path. But the problem is what's
being written: the first part of the file is the byte array that I'm
trying to write, but the rest is my page's html (see below for sample
output). I thought that setting the content-length header to the
length of the byte array would force it to only write that many bytes,
but it still writes 1.12Kb regardless.
I just don't know what else to try; any ideas would be appreciated.
Sample output (the HIPQ is the contents of the byte array):
HIPQ
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>ViewAllocations</title>
..
..
..
etc.
I'm trying to download some content as a file to the client. My code
behind looks like this:
private void DownloadFile(byte[] contents)
{
Response.ContentType = "text/csv";
Response.AppendHeader("content-disposition",
"attachment;filename=myFile.csv");
Response.AppendHeader("content-length",contents.Length.ToString());
Response.BinaryWrite(contents);
}
It works to an extent, in that it pops up the file download dialog and
writes a file to the user's selected path. But the problem is what's
being written: the first part of the file is the byte array that I'm
trying to write, but the rest is my page's html (see below for sample
output). I thought that setting the content-length header to the
length of the byte array would force it to only write that many bytes,
but it still writes 1.12Kb regardless.
I just don't know what else to try; any ideas would be appreciated.
Sample output (the HIPQ is the contents of the byte array):
HIPQ
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>ViewAllocations</title>
..
..
..
etc.