Upload and Download File From Database Using GridView

Introduction: In this Article we will learn how to upload files to and download files from a database. First we save user resume details and upload resume in the database. Then we display saved information in a GridView and download the selected resume.

Step 1: First create the database in SQL; give the name of the database as TestDatabase and the table name as ResumeDetails. Define the name of columns in the table, like this:

Clipboard102.jpg

Step 2: Create a new web page Default.aspx in Visual Studio, then add Textboxes, Labels, DropDownList and FileUpload from Tools, like this:

Clipboard02.jpg

Use the FileUpload tool to upload a file and DropDownList to select country.

Step 3: Enter the code into the Default.aspx.cs file to input values, then click the Save button to save the values in the database table. Also put in the condition that the file to upload should be text, pdf or docx.
 
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Data;
using
System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    SqlCommand cmm;
    SqlConnection con;
    SqlDataAdapter da;
    string s;
    protected void Page_Load(object sender, EventArgs e)
    {
       cmm = new SqlCommand("Insert into ResumeDetail values(@a,@b,@c,@d,@e,@f,@g,@h,@k,@i,@j,@l)");
       con = new SqlConnection("Data Source=.;Initial Catalog=TestDatabase;uid=sa;Pwd=wintellect");
       cmm.Connection = con;
       da = new SqlDataAdapter(cmm);
       
        if (FileUpload1.HasFile)
            {
                string fileExt = System.IO.Path.GetExtension(FileUpload1.FileName);

                if (fileExt == ".pdf" || fileExt == ".txt" || fileExt == ".docx")
                {
                    s = MapPath("img");
                    s = s + "/" + FileUpload1.FileName;
                    FileUpload1.SaveAs(s);
                }
                else
                {
                    Response.Write("Invalid Upload file format");
                }
            }   
      
            cmm.Parameters.AddWithValue("@a", TextBox2.Text);
            cmm.Parameters.AddWithValue("@b", TextBox3.Text);
            cmm.Parameters.AddWithValue("@c", TextBox4.Text);
            cmm.Parameters.AddWithValue("@d", TextBox5.Text);
            cmm.Parameters.AddWithValue("@e", TextBox6.Text);
            cmm.Parameters.AddWithValue("@f", TextBox7.Text);
            cmm.Parameters.AddWithValue("@g", TextBox8.Text);
            cmm.Parameters.AddWithValue("@h", TextBox9.Text);
            cmm.Parameters.AddWithValue("@i", TextBox10.Text);
            cmm.Parameters.AddWithValue("@j", TextBox11.Text);
            cmm.Parameters.AddWithValue("@k", DropDownList1.SelectedValue);
            cmm.Parameters.AddWithValue("@l", s);
    }

    protected void Button1_Click(object sender, EventArgs e)
    {       
        con.Open();
        cmm.ExecuteNonQuery();
        con.Close();
    }

Step 4: After clicking the save button our database Table look like this:

Clipboard04.jpg


Step 5: Now create a new webpage, Downloads.aspx, to show the saved data by using GridView. Select Data Sources from the GridView smart tag to select the ResumeDetail database table.

Clipboard07.jpg

Step 6: Again click on the GridView Tasks (Smart Tag) to add a new column, then choose the field type as ButtonField and Type as Download.

Clipboard08.jpg

Clipboard09.jpg

Step 7: Click on GridView Tasks to Edit column and assign the CommandName as download in the ButtonField properties.

Clipboard10.jpg

Step 8: Enter code into the Download.aspx.cs file, like this:

using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Data.SqlClient;
using
System.Data;
using
System.Net;

public
partial class Downloads : System.Web.UI.Page
{
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "download")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            string url = GridView1.Rows[index].Cells[13].Text;
            System.IO.FileInfo file = new System.IO.FileInfo(url);
            if (file.Exists)
            {
                Response.Clear();
                Response.AppendHeader("Content-Disposition:", "attachment; filename=" + file.Name);
                Response.AppendHeader("Content-Length", file.Length.ToString());
                Response.ContentType = "application/octet-stream";
                Response.TransmitFile(file.FullName);
                Response.End();
            }
            else
            {
                Response.Write("NO FILE PRESENT");
            }
        }
    }
}

Step 9: Now run the application by pressing F5 and the output is as:

Clipboard05.jpg

Click on any file you want to download.

Clipboard6.jpg


Similar Articles