Click here to Skip to main content
15,879,326 members
Articles / Web Development / ASP.NET
Tip/Trick

File Upload and Download in ASP.NET

Rate me:
Please Sign up or sign in to vote.
3.31/5 (21 votes)
5 May 2011CPOL 121.7K   16   9
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)


Written By
Software Developer India
India India
http://devcorners.com/
Total DotNet/Programming Solution

I am Prasanta Banerjee. I am an Asp.Net Developer. My site: http://devcorners.com/
Email: prasanta.it@hotmail.com
If any body wants to prepare for interview http://guru-code.blogspot.com/ is the good site.

Comments and Discussions

 
BugDownload code not working Pin
_Dhull 29-Jan-14 21:57
_Dhull 29-Jan-14 21:57 
Questionin case of .xls and in firefox this is not working Pin
V1CT0R.15-Dec-13 23:05
V1CT0R.15-Dec-13 23:05 
Questionhow to download uploaded file Pin
manikantaer12-Jul-12 18:35
manikantaer12-Jul-12 18:35 
Generalcan this code be used to download any kind of file Pin
ashishmaniyar13-Nov-11 19:06
ashishmaniyar13-Nov-11 19:06 
Generalchange code line Pin
zSegundo2-May-11 13:29
zSegundo2-May-11 13:29 
Generalur code for file download Pin
Himil Modi26-Apr-11 19:47
Himil Modi26-Apr-11 19:47 
Answer code for file download from server Pin
Debasis hota-Dot Net Devloper28-Aug-13 5:29
Debasis hota-Dot Net Devloper28-Aug-13 5:29 
C#

---------->
//Take BulletedList1 in page design & Write Folloing in .aspx.cs For Downloading

protected void Page_Load(object sender, EventArgs e)
{

DirectoryInfo dinfo = new DirectoryInfo(Server.MapPath("."));
BulletedList1.DataSource = dinfo.GetFiles();
BulletedList1.DataBind();

}

//Write Folloing in .aspx.cs For uploading----->
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
Response.Write(FileUpload1.PostedFile.ContentLength>0);
if (FileUpload1.PostedFile.ContentLength>0)
{
string p = Server.MapPath(".")+"\\"+FileUpload1.FileName;
FileUpload1.PostedFile.SaveAs(p);
Label1.Text = "successfully uploaded";
}
else Label1.Text = "upload file";

}
else Label1.Text = "select file";
}
GeneralWhat happens if... Pin
Henry Minute24-Apr-11 11:31
Henry Minute24-Apr-11 11:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.