Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Downloading file to client

Reply
Thread Tools

Downloading file to client

 
 
Cathryn Johns
Guest
Posts: n/a
 
      10-26-2005
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.

 
Reply With Quote
 
 
 
 
Chris Botha
Guest
Posts: n/a
 
      10-26-2005
After Response.BinaryWrite add the statement
Response.End()

"Cathryn Johns" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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.
>



 
Reply With Quote
 
 
 
 
HIK
Guest
Posts: n/a
 
      10-26-2005
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 Botha wrote:
> After Response.BinaryWrite add the statement
> Response.End()
>
> "Cathryn Johns" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) oups.com...
>
>>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.
>>

>
>
>

 
Reply With Quote
 
CJ
Guest
Posts: n/a
 
      11-02-2005
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 Botha wrote:
> After Response.BinaryWrite add the statement
> Response.End()
>
> "Cathryn Johns" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) oups.com...
> > 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.
> >


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Downloading file from Web Server to Client cbanks@bjtsupport.com ASP .Net 1 12-03-2005 08:54 AM
Downloading/opening file in client application window Rob Nicholson ASP .Net 3 05-13-2005 02:26 AM
Smart Client Downloading -Sattelite Assembiles =?Utf-8?B?VmVua2F0IEthc2FyYW0=?= ASP .Net 1 03-10-2005 12:45 PM
Client file downloading........ RC ASP .Net 3 01-19-2005 09:33 AM
Downloading a file to client Ian Gordon ASP .Net 2 07-30-2003 10:15 PM



Advertisments