Click here to Skip to main content
15,900,108 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I need to write a text file and download that file. I used this following code, but it is not working :
C#
string f1 = Path.GetTempPath() + "Activity.txt";
            FileInfo f2 = new FileInfo(f1);
            if (File.Exists(f2.Name))
            {
                File.Delete(f1);
            }
            StreamWriter sw = File.CreateText(Path.GetTempPath() + "Activity.txt");
            sw.WriteLine();
            sw.WriteLine();
            sw.WriteLine("\t" + "\t" + "\t\t\Binderab City");
           
          
            sw.Close();
            Response.Clear();
            Response.AddHeader("content-disposition", "attachment; filename=" + f2.Name);
            Response.AddHeader("content-length", f2.Length.ToString());
            Response.ContentType = "Application/Org.txt";
            Response.WriteFile(f2.FullName);
            Response.End();
            Response.Flush();
Posted
Updated 10-May-12 5:38am
v2
Comments
Sandeep Mewara 10-May-12 11:39am    
Elaborate what do you mean by 'not working'?
Sergey Alexandrovich Kryukov 10-May-12 13:31pm    
I can guess, please see my answer.
But you are right: a software developer should not say "it is not working" instead of providing comprehensive issue report.
--SA

1 solution

"Write" and "download" a file are mutually exclusive. It's a client part which downloads something. What I can see from your code is different: in response to client's HTTP request, your server side writes and creates temporary file, then this file is sent in HTTP request to the client which downloads it.

In principle, this code should work — almost, but it's full of some absurdities. There is no such content types. You probably mean "text/plain". The real problem is: you don't dispose StreamWriter, so you do not guarantee that your file is saved; it's the best to use using statement (don't mix up with using clause!). Getting FullName is redundant.

First of all, as this is nothing more than a simple text file which could be transferred in ASCII encoding, so you don't even need a file. You could write directly in your response:
C#
Response.Clear();
Response.AddHeader("content-disposition", "attachment; filename=some-file.txt");
Response.ContentType = "text/plain";
Response.Write();
Response.Write();
Response.Write("\t" + "\t" + "\t\t\Binderab City");
Response.End();


This way, a client side can always download some file, which is not physically a file on the server side.

You can write a file, of course, but it makes sense if you want to write something more complex. As a bonus, I'll demonstrate sending a file with UTF-8 encoding with BOM, so you could use non-ASCII Unicode code points and read the downloaded text file with any Unicode-enabled text editor with standard behavior — recognition of encoding by BOM:

C#
Response.ContentEncoding = System.Text.Encoding.UTF8;

string fileName = string.Format("{0}{1}", System.IO.Path.GetTempPath(), "Activity.txt");

using (System.IO.StreamWriter writer = new System.IO.StreamWriter(fileName, false, Response.ContentEncoding)) {
    writer.WriteLine();
    writer.WriteLine("...");
} //Important! writer.Dispose is called automatically here

Response.Clear();
Response.AddHeader("content-disposition", "attachment; filename=some-file.txt");
Response.ContentType = "text/plain";
Response.Charset = "utf-8";
Response.WriteFile(fileName);
Response.End();
// and think about doing this, sooner or later:
System.IO.File.Delete(fileName);


See also:
http://en.wikipedia.org/wiki/Byte_order_mark[^],
http://unicode.org/faq/utf_bom.html[^],
http://msdn.microsoft.com/en-us/library/yh598w02.aspx[^].

—SA
 
Share this answer
 
v7
Comments
Sandeep Mewara 11-May-12 0:42am    
5+ SA!
Sergey Alexandrovich Kryukov 11-May-12 10:37am    
Thank you, Sandeep.
--SA

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900