im trying to return a file from a request

  • Thread starter Thread starter Sam Martin
  • Start date Start date
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
 
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!
***************************
 
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
 
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.
 
Hello Sam,

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

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

all sorted now. (worked perfectly)

much appreciated

sam
 
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

Back
Top