How to download the uploaded files using ASP.NET?
In this article I have explained about how to Upload and download files from server. In this application I have used file upload control for upload files to server. The uploaded details are stored in the SQL server table and manipulate those details using Grid View. If user wants to delete file then easily delete files using grid view delete button.
How to Upload files to Server path?
First we can check whether that file name is available in the server uploaded path and if not available then we upload that file.
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
fname = FileUpload1.FileName;
spath = @"~\Uploaded\" + FileUpload1.FileName;
fpath = Server.MapPath("Uploaded");
fpath = fpath + @"\" + FileUpload1.FileName;
desc = TextBox2.Text;
if (System.IO.File.Exists(fpath))
{
Label1.Text = "File Name already exists!";
return;
}
else
{
FileUpload1.SaveAs(fpath);
}
//Store details in the SQL Server table
StoreDetails();
TextBox2.Text = "";
//Refresh grid
LoadGrid();
}
else
{
Label1.Text="Please select file!";
}
}
void StoreDetails()
{
String query;
query = "insert into fileDet(fname,fpath,desc1) values('" + fname + "','" + spath + "','" + desc + "')";
sqlcon.Open();
sqlcmd = new SqlCommand(query, sqlcon);
sqlcmd.CommandType = CommandType.Text;
sqlcmd.ExecuteNonQuery();
sqlcon.Close();
LoadGrid();
}
void LoadGrid()
{
sqlcon.Open();
sqlcmd = new SqlCommand("select * from fileDet", sqlcon);
da = new SqlDataAdapter(sqlcmd);
dt.Clear();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
GridView1.DataSource = dt;
GridView1.DataBind();
}
else
{
GridView1.DataBind();
}
sqlcon.Close();
}
How to delete file details in SQL server table and delete the file in server path when user click delete button in the grid view?
We can use System.IO.File.Delete option to delete the file from server path
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
String ID;
ID = GridView1.DataKeys[e.RowIndex].Value.ToString();
sqlcmd = new SqlCommand("select * from fileDet where ID='" + ID + "'", sqlcon);
sqlcon.Open();
da = new SqlDataAdapter(sqlcmd);
da.Fill(dt);
if (dt.Rows.Count > 0)
{
if (System.IO.File.Exists(Server.MapPath(dt.Rows[0][2].ToString())))
{
System.IO.File.Delete(Server.MapPath(dt.Rows[0][2].ToString()));
}
}
sqlcmd = new SqlCommand("delete from fileDet where ID='" + ID + "'", sqlcon);
sqlcmd.ExecuteNonQuery();
sqlcon.Close();
LoadGrid();
}
How to download uploaded file from server path?
During uploaded time i have store the server path in the SQL Server table. Then bind in grid view If user click the file name in the grid view then files are downloaded. I had bind server path in Grid View Hyper link (Navigate URL).
Source Code Detail:
Here with I have attached source code for upload/download files from/to Server location and also maintain details in SQL Server table using grid view. Download it and try to upload/download files.
Front End : ASP.NET
Code Behind : C#
Conclusion:
I hope this Article is help to you for understand file upload to Server path Concept.
Hi Ravindran,
Good day! Thanks for this help and I really appreciate it.. Just want to ask if there's a way to view the uploaded file in a new page even if i don't download it? Please help me I'm new to this industry please help me.. I also want to use the view button in gridview.
Please help me..:(
Thanks in advance!