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")
 
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();
 
Back
Top