Click here to Skip to main content
Sign Up to vote bad
good
See more: C#ASP.NET
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)
 
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 9 Dec '12 - 19:32
Hesha746
Edited 9 Dec '12 - 19:54


2 solutions

Hi use this
 
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();
  Permalink  
Try this
 
 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();
}
  Permalink  
Comments
Hesha - 10 Dec '12 - 3:06
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:18
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:11
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:16
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)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 414
1 Arun Vasu 223
2 OriginalGriff 190
3 CPallini 163
4 Aarti Meswania 158
0 Sergey Alexandrovich Kryukov 10,169
1 OriginalGriff 7,749
2 CPallini 4,181
3 Rohan Leuva 3,482
4 Maciej Los 3,089


Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 10 Dec 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid