Click here to Skip to main content
15,878,748 members
Please Sign up or sign in to vote.
4.33/5 (2 votes)
See more:
I have some files(txtfile) stored on my Server machine Folder. When a user wants to download This File. This Button can be used by users to download the file.

How to create this Button?

Thank you ...
Posted
v2
Comments
ravikhoda 28-Mar-14 5:44am    
well you need to add the button on your web page and on button click you can write a code for file download.
[no name] 28-Mar-14 6:10am    
yes
Ankur Ramanuj 29-Mar-14 8:57am    
your source is on same server?

 
Share this answer
 
 
Share this answer
 
Comments
[no name] 28-Mar-14 6:03am    
Am facing same problem.My Text file Genarate Every day.I create file name

string date = DateTime.Now.ToShortDateString();
string newdate = date.Replace("/", ".");

File name is " + newdate + ".TXT"

How to applly this file name your Code,i try this one

Response.AppendHeader("Content-Disposition", "attachment; filename="+ newdate +".TXT");
Response.TransmitFile(Server.MapPath("~/C:/HKGOUTBOUND/"+ newdate + ".TXT"));


it's not working
vsrikanth87 28-Mar-14 6:10am    
check this

http://www.aspdotnet-suresh.com/2012/02/saveupload-files-in-folder-and-download.html
vsrikanth87 28-Mar-14 6:16am    
have a look at it this might help u by downloading the code and u can view demo

http://www.aspsnippets.com/Articles/Download-Files-from-GridView-using-LinkButton-Click-Event-in-ASPNet-using-C-and-VBNet.aspx
This is so simple

..a href="C:\GiveYourPath\txtfile.txt">Download Here
you need to mention your file path it will give you download link
complete your href link
how-to-use-the-download-attribute/[^]
download_link.htm[^]
 
Share this answer
 
v3
Place the below code in your button:

C#
string fileName = "Your Filename";// Replace Your Filename with your required filename

        Response.ContentType = "application/octet-stream";

        Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);

        Response.TransmitFile(Server.MapPath("~/YourFolder/" + fileName));//Place "YourFolder" your server folder Here

        Response.End();
 
Share this answer
 
Comments
[no name] 31-Mar-14 1:39am    
i try this one not Working,where is the error.
string date = DateTime.Now.ToShortDateString();
string newdate = date.Replace("/", ".");
string filePath = ""+ newdate + ".TXT";

Response.ContentType = "Application/TXT";
Response.AppendHeader("Content-Disposition","attachment;filename="+filePath);
Response.TransmitFile(Server.MapPath("~/C:/HKGOUTBOUND/"+ filePath));
Response.End();
Ajay_Babu 1-Apr-14 0:21am    
which error it shows?
<a href="../sample.txt">sample</a>
 
Share this answer
 
if your source and files are on same server than its simple just give the path in below code

C#
Response.ContentType = "application/octet-stream";
           Response.AddHeader("Content-Disposition", "attachment;filename=filename.txt");
           Response.WriteFile(path);
           Response.End();


But if your files which you want to download is on other server than host the folder which contains all your files and give the path in above code.
 
Share this answer
 

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