Downloading file to client

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.
 
H

HIK

I've found it best to open the file in a new page.
i.e. Response.redirect("Downloadpage.aspx",true)

I put the download code in the new page dedicated just for this purpose.




Chris said:
After Response.BinaryWrite add the statement
Response.End()

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.
 
C

CJ

That worked beautifully, when the method was called from an asp.net
button click. Now it has to be called from a javascript menu system,
so I've put the method on the OnPageLoad handler.

Response.End doesn't work anymore; I get a "Thread was being aborted"
exception. Even though I put it right at the end of my asp.net method,
there is some javascript that runs after the method returns, and I
assume that this is what is interfering with Response.End.

Is there any other way to do this?

Chris said:
After Response.BinaryWrite add the statement
Response.End()

Cathryn Johns said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top