Sending a file from memory to the client

J

John Owens

Currently I use Response (below) but it feels a little hacky. Is there
anyway to do this using a nice little Framework class :)

Response.Clear();

Response.AddHeader("Content-Disposition", "attachment;
filename=Letter.blah");

Response.AddHeader("Content-Length", msOutFileData.Length.ToString());

Response.ContentType = "application/vnd.msword";

Response.BinaryWrite( msOutFileData.GetBuffer() );

Response.Flush();



Response.Redirect( "" );
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,


no AFAIK, what is wrong with Response?


I would change the Redirect at the end by a Response.End(), other than that
all looks ok

cheers,
 
J

John Owens

I thought it might have been wrapped in a nice class.

I also I'm having problems as its not asking for SaveAs. It displays a
dialog box that only asks for Open and Save.

Any idea what causes this as I have tried multiple different Response setups
below is one:

Response.Buffer = true;

Response.ContentType = "application/msword";

Response.AddHeader( "Content-Disposition", "attachment;");

Response.AddHeader( "Content-Length", msOutFileData.Length.ToString());
 
I

Ignacio Machin \( .NET/ C# MVP \)

hi,

I think this is the default way, it's cause you have checked the "always ask
before open this kind of file checkbox "

I do it in almost the same way:
Response.AddHeader("Content-Disposition", "attachment;
filename=\""+targetname + "\"");

The only different is that I suggest a filename of the downloaded file.


cheers,
 
J

John Owens

Thanks.

Yea, I have tried using a filename. It still won't give me the option to
change it.

Don't worry, I've made it inline (so it opens in the browser) and this
behaviour is acceptable.
 
J

John Owens

I'm having another problem where it places junk at the end of the file
whenever I save. Any ideas what might cause that.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,


no , no idea

Check the part where you create/fill the stream.

John Owens said:
I'm having another problem where it places junk at the end of the file
whenever I save. Any ideas what might cause that.


cheers,
 

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

Top