im trying to return a file from a request

S

Sam Martin

Hi all,

One of my pages needs to return a CSV file.

I've set the Response.ContentType = "text/plain"; but I don't want the
browser displaying the file, instead I want the user to get the file save
dialog

Anyone know how to do this?

TIA
Sam Martin
 
G

Guest

text/plain is designed to show up in the browser. You can, potentially, save
the resulting csv instead of trying to stream it out. You then reidrect to
that file (popup window is easiest). NOTE, however, that the user may have
Excel set up to edit from the browser, so it picks up the CSV instead. AFAIK,
there is nothing you can do about the user's browser settings.


---

Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************
 
K

Kevin Spencer

One alternative is to put the CSV file into a Zip file. Zip files are always
prompted.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
I get paid good money to
solve puzzles for a living
 
G

Guest

Hi,

It is possible if you can stream it out ur input file. The below mentioned
code will help u out......

long fileSize=0;
fileStream = new FileStream("your_CSV_InputFileName.csv", FileMode.Open);
fileSize = fileStream.Length;
byte[] buffer = new byte[(int)fileSize];
fileStream.Read(buffer, 0, (int)fileSize);
string strAttachment="attachment; filename=abc.CSV";
Response.Clear();
Response.AddHeader("Content-Disposition: ",strAttachment);
Response.ContentType = "text/plain";
Response.BinaryWrite(buffer);
fileStream.Close();

Regards,
Kamal T.
 
A

Ariel Popovsky

Hello Sam,

Try adding this header to your response:
"Content-Disposition: attachment"

using
Response.AppendHeader("Content-Disposition", "attachment;filename=xxxxxx.csv")
 
S

Sam Martin

more specifically, if anyone else tries to do this.....

string fileName =
Convert.ToString("csvdump_"+System.DateTime.Now.ToShortDateString()+".csv").Replace("/","");;

byte[] buffer = System.Text.Encoding.ASCII.GetBytes(
<GET_MY_CSV_STRING()> );

string strAttachment="attachment; filename="+fileName;
Response.Clear();
Response.AddHeader("Content-Disposition: ",strAttachment);
Response.ContentType = "text/plain";
Response.BinaryWrite(buffer);

Response.End();
 

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
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top