65.9K
CodeProject is changing. Read more.
Home

File Upload and Download in ASP.NET

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.31/5 (21 votes)

Apr 23, 2011

CPOL
viewsIcon

123187

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

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

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();