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 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:32pm
Hesha764
Edited 9-Dec-12 19:54pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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  
v3
Comments
Hesha at 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 at 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 at 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 at 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)

  Print Answers RSS
0 OriginalGriff 205
1 Sergey Alexandrovich Kryukov 165
2 jlopez788 109
3 Rob Philpott 100
4 Prakriti Goyal 97
0 OriginalGriff 6,837
1 Sergey Alexandrovich Kryukov 5,589
2 Maciej Los 3,479
3 Peter Leow 3,323
4 DamithSL 2,505


Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 10 Dec 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