views:

362

answers:

2

Hi,

Pardon the dumb newbie question here; web programming isn't my forte... (blush)

I have an aspx page running on a web server. I have a blob (byte array) containing any kind of binary file, plus a file name.

I would like to push this file to be downloaded through the browser onto the client, and opened using whatever application is default for this file type. I really don't want to save the blob as a file on the server; that will leave a terrible housekeeping mess that I just don't want to think about.

I did try googling this question, but I guess I'm using the wrong keywords.

This really should be obvious how to do it, but I'm having no joy.

What is the trick?

Thanks!

+7  A: 
Response.BinaryWrite(byteArray);

You should also set the content type

Response.ContentType = "application/pdf";

But that will be based on your file type.

And the file name (and everything together) is done like this

Response.AddHeader("content-disposition", 
   String.Format("attachment;filename={0}", fileName));    
Response.ContentType = "application/pdf";
Response.BinaryWrite(byteArray);
Bob
I would add that certain browsers (*cough*IE*cough) often ignore the filename.
Robert C. Barth
Ohh yeah, I have ran in to that before. One trick that also sometimes works is to add the file name to the end of the querystring.
Bob
Thank you Bob for your quick, succinct and accurate response!
Shaul
+3  A: 

First, you have to know the mime type. Once you know that, you can set the Response.ContentType property. After that, just use Response.BinaryWrite(). If you don't first set the ContentType property, the client will have almost no chance of opening the file correctly.

Robert C. Barth