.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal

Home >> Code Snippets >> ASP.NET Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Simple Way To Upload File and Download Them

Posted By:Hefin Dsouza       Posted Date: August 13, 2009    Points: 10    Category: ASP.NET Controls    URL: http://www.dotnetspark.com  

This Demonstration Shows How To Upload Files on a Server and How To allow Users to Download Them. Can be Used To Develop sites like RapidShare.com etc...

Files Are Stored Using Random Names.
Code TO Upload

if (FileUpload1.FileName != "")
FileUpload1.PostedFile.SaveAs(Server.MapPath("uploads") + "\\" + ran.Next() + FileUpload1.FileName);//ran is an object of Random class to generate random numbers.

Code TO Download
Code is used to get all files and store it in an Array
 string[] fileName=Directory.GetFiles(Server.MapPath("uploads"));

Using a For Each Loop to display the Files on the Page.

foreach (var item in fileName)
data+=@"< tr >< td >"+cnt.ToString()+"< td >"+item.Remove(0,item.LastIndexOf(@"\")+1)+"< /td >< td >< a href="+item+" >Click Here< / a >< / td >";
Regards Hefin Dsouza


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend