File Transfer To Client

D

DavidJCouture

Hi,

I have the following situation which I assume is a common one.
The user clicks a button to request a report. The report I produce is
a simple .csv text file. I want the report file to be transferred to
the client's machine presumably by bringing up the standard download
dialog for whichever browser they're using. What is the best way to
accomplish this using C# and .NET 2.0?

Thanks in advance.
 
C

CMM

Look at Response.BinaryWrite(...).
Also, you might want to use Response.Append("Content-Disposition",
"attachment;filename=xxxx.xxx" ....) to add the filename.
 
C

Colby Africa

Here is a snippet I use to transmit an Excel file to the client:

Response.ContentType = "application/vnd.ms-excel";
Response.ContentEncoding = System.Text.Encoding.Default;
Response.AppendHeader("Content-Disposition", @"attachment; filename="
+ Path.GetFileName(filePath));
Response.TransmitFile(filePath);

Hope this helps.

Colby
http://colbyafrica.blogspot.com
 
D

DavidJCouture

Here is a snippet I use to transmit an Excel file to the client:

Response.ContentType = "application/vnd.ms-excel";
Response.ContentEncoding = System.Text.Encoding.Default;
Response.AppendHeader("Content-Disposition", @"attachment; filename="
+ Path.GetFileName(filePath));
Response.TransmitFile(filePath);

Hope this helps.

Colbyhttp://colbyafrica.blogspot.com

Thank you very much! Using your code as a starting point I did a
little searching and came up with the following:

//---Send the file to the client
Response.Buffer = true;
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "text/plain";
Response.AddHeader("Content-Disposition", @"attachment;
filename=" +
Path.GetFileName(reportFilename));
Response.TransmitFile(reportFilename);
Response.End();

I am very grateful that there are selfless, generous people like you
in the world. Thanks again for the help.

Dave Couture
 
C

Colby Africa

Thank you very much! Using your code as a starting point I did a
little searching and came up with the following:

//---Send the file to the client
Response.Buffer = true;
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "text/plain";
Response.AddHeader("Content-Disposition", @"attachment;
filename=" +
Path.GetFileName(reportFilename));
Response.TransmitFile(reportFilename);
Response.End();

I am very grateful that there are selfless, generous people like you
in the world. Thanks again for the help.

Dave Couture

My pleasure!
 
C

CMM

I've also taken to keeping a somewhat exhaustive dos.extension-to-mime.type
table that I populate a dictionary with... so that I can dynamically set
Response.ContentType without knowing the filetype beforehand. You can find
some of these tables by doing google search. Here's an example:
http://silk.nih.gov/public/[email protected]. Windows has these
tucked away somewhere too.... but I never quite figured out how to access
it.
 
C

Colby Africa

Hey there. MIME data is stored in the registry. Check the key:

HKEY_CLASSES_ROOT\MIME\Database\Content Type

Colby
 
C

CMM

I don't remember what the problem was with this other than the roundabout
nature of working with it (first you need to lookup the dos .xxx extension
in HKCR... which may or may not exist). I'm also not sure if an ASP.NET has
access to these keys. Probably. Anyway, maintaining your own table (I keep
mine in a comma delimited text file that I read at app startup) is fairly
painless. :)
 

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