Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
Hi ,
 
I should create a .txt file dynamically . After the text file is created, it should be downloaded by the user. For this purpose , I try the code (I simplified the code basically like below ):
string blabla= "Hello World";
Response.Clear();
Response.ContentType = "text/plain";
Response.AddHeader("Exported text File", "attachment; filename=helloWorld.txt");
Response.ContentEncoding = Encoding.Default;
byte[] bytes = Encoding.Default.GetBytes(blabla);
Response.BinaryWrite(bytes);
Response.End();
But this code block behaves like Response.Write() . The content is written at the download page. There is no file to be downloaded.
 
Thanks for the replies in advance..
Posted 4-Sep-12 21:38pm
Edited 4-Sep-12 21:39pm
_Amy51.3K
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Try adding Content-Disposition in header.
Try this:
string blabla = "Hello World";
Response.Clear();
Response.ContentType = "text/plain";
//Response.AddHeader("Exported text File", "attachment; filename=helloWorld.txt");
Response.AddHeader("Content-Disposition", String.Format("attachment;filename=helloWorld.txt"));
Response.ContentEncoding = Encoding.Default;
byte[] bytes = Encoding.Default.GetBytes(blabla);
Response.BinaryWrite(bytes);
Response.End();
 

 
--Amit
  Permalink  
v2
Comments
no_-_namee at 5-Sep-12 3:08am
   
Thanks for the reply :)
It works well, by the way what is the difference of "Content-Disposition" in header?
_Amy at 5-Sep-12 3:16am
   
It forces the browser to open save as dialogue with the attached file. Refer here[^].

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 274
1 Sergey Alexandrovich Kryukov 255
2 Shweta N Mishra 216
3 PIEBALDconsult 200
4 BillWoodruff 174
0 OriginalGriff 7,630
1 Sergey Alexandrovich Kryukov 7,022
2 DamithSL 5,586
3 Manas Bhardwaj 4,946
4 Maciej Los 4,525


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 5 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100