Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

File Upload and Download in ASP.NET

3.31/5 (21 votes)
5 May 2011CPOL 123.2K  
ASP.NET sample code for uploading and downloading files
There are lots of situation, Where we have to use file uploader in our project. But beginners don’t get proper code for Uploading and Downloading File. Using this code they can do it:

File Upload Code



C#
string FilePath = "";
string[] a = new string[1];
string fileName = "";
string FullName = "";

if (FileUploader.FileName.Length > 0)
{
     a = FileUploader.FileName.Split('.');
     fileName = Convert.ToString(System.DateTime.Now.Ticks) + "." + a.GetValue(1).ToString();
     FilePath = Server.MapPath(@"~\SavedFolder");
     Fup1.SaveAs(FilePath + @"\" + fileName);

     FullName = FilePath + @"\" + fileName;
     // Database Saved Code
}

File Download Code


C#
string filename = "filename from Database";
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + filename);
string aaa = Server.MapPath("~/SavedFolder/" + filename);
Response.TransmitFile(Server.MapPath("~/SavedFolder/" + filename));
Response.End();

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)