Click here to Skip to main content
15,888,401 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HI everyone
In my asp.net application , i have created text data file and save that file in the server. but i want to give pop up window to the Client and allow Clients to download that particular text file to their PC.

i used following Code :

// String Mappath is the actual path where text file's actual Location.what i want is give users to download that textfile from the path(mappath)

C#
Response.ContentType = "text/plain";
              String fileName = "HKG- OUTBOUND " + newdate + ".TXT";
              Response.AppendHeader("Content-Download", "attachment; filename='" + fileName + "'");

              String Mappath = "C:\\Users\\Heshan\\Desktop\\TEXTFILES\\HKG- OUTBOUND " + newdate + ".TXT";


                Response.TransmitFile(Mappath);

              Response.End();



But it did nt worked for me. can someone assist me on that matter that would be a great help for me.
Thanks in Advance!!!
Posted
Updated 9-Dec-12 19:54pm
v2

Hi use this

C#
Response.ClearContent();
                    Response.ClearHeaders();
                    Response.AddHeader("Content-Disposition", "attachment;filename=" + Filename);
                    Response.ContentType = "application/excel";
                    Response.WriteFile(Server.MapPath(File path name));
                    Response.Flush();
                    Response.Clear();
                    if (File.Exists(Server.MapPath(File path name)))
                    {
                        File.Delete(Server.MapPath(File path name));
                    }
                    Response.Close();
 
Share this answer
 
v2
Try this

C#
 string filepath = Server.MapPath("test.txt");  
   FileInfo file = new FileInfo(filepath); 
   if (file.Exists)
   {    
    Response.ClearContent();   
    Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);  
   
    Response.AddHeader("Content-Length", file.Length.ToString());   
    Response.ContentType = "text/plain";    
    Response.TransmitFile(file.FullName); // for large file
// Or
// Response.WriteFile(file.FullName);     // for small file
    Response.End();
}
 
Share this answer
 
v3
Comments
Hesha 10-Dec-12 3:06am    
Hm i tried it But i did nt get any response buddy. in debug no errors but did nt get the pop up.
pradiprenushe 10-Dec-12 4:18am    
Give corrcet path of file. I think you may be giving incorrect path so debugger not going in if condition.
Hesha 10-Dec-12 5:11am    
No buddy. i think path is ok.it go thought IF condition.since i know the path i gave the path of the TXT file.i did nt used map function..sample caode i used is as follows

if there is any error please let me know. Thanks in advance!


}
sw.Close();

String path = @"C:\\Users\\Heshan\\Desktop\\TEXTFILES\\HKG- OUTBOUND " + newdate + ".TXT";

//string filepath = Server.MapPath("test.txt");
FileInfo file = new FileInfo(path);
if (file.Exists)
{
Response.ClearContent();
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);

Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "text/plain";
//Response.TransmitFile(file.FullName); // for large file
// Or
Response.WriteFile(file.FullName); // for small file
Response.End();




}
pradiprenushe 10-Dec-12 5:16am    
Are you using update panel in page?

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