Tuesday, 18 June 2013

How to download the uploaded files using ASP.NET?

How to download the uploaded files using ASP.NET?


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.
Design:

Table:

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). 

No comments:

Post a Comment