In an ASP.NET project we need to upload and download files. For that I am using SQL Server and a GridView. The GridView shows that list of uploaded files with the file name and file type and provide the Download link for each file.
Use the following the procedure.
Open Visual Studio 2008 then select "File" -> "New" -> "Project...".
Create a database table named “FileInformation”.
Design and Coding
Design
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- <script type="text/javascript">
- function myfunction() {
- alert("Please Upload File");
- }
- </script>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <asp:FileUpload ID="fileUpload1" runat="server" /><br />
- <asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click" />
- </div>
- <div>
- <asp:GridView ID="gvDetails" runat="server" AutoGenerateColumns="false" DataKeyNames="Id">
- <HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
- <Columns>
- <asp:BoundField DataField="Id" HeaderText="Id" />
- <asp:BoundField DataField="FileName" HeaderText="FileName" />
- <asp:TemplateField HeaderText="FilePath">
- <ItemTemplate>
- <asp:LinkButton ID="lnkDownload" runat="server" Text="Download" OnClick="lnkDownload_Click"></asp:LinkButton>
- </ItemTemplate>
- </asp:TemplateField>
- </Columns>
- </asp:GridView>
- </div>
- </form>
- </body>
- </html>
Code
- string strCon = "Data Source=(local);Integrated Security=true;Initial Catalog=Test";
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)
- {
- BindGridviewData();
- }
- }
-
-
- private void BindGridviewData()
- {
- using (SqlConnection con = new SqlConnection(strCon))
- {
- using (SqlCommand cmd = new SqlCommand())
- {
- cmd.CommandText = "select * from FileInformation";
- cmd.Connection = con;
- con.Open();
- gvDetails.DataSource = cmd.ExecuteReader();
- gvDetails.DataBind();
- con.Close();
- }
- }
- }
- protected void btnUpload_Click(object sender, EventArgs e)
- {
- if (fileUpload1.HasFile)
- {
- string filename = Path.GetFileName(fileUpload1.PostedFile.FileName);
- Stream str = fileUpload1.PostedFile.InputStream;
- BinaryReader br = new BinaryReader(str);
- Byte[] size = br.ReadBytes((int)str.Length);
- using (SqlConnection con = new SqlConnection(strCon))
- {
- using (SqlCommand cmd = new SqlCommand())
- {
- cmd.CommandText = "insert into FileInformation(FileName,FileType,FileData) values(@Name,@Type,@Data)";
- cmd.Parameters.AddWithValue("@Name", filename);
- cmd.Parameters.AddWithValue("@Type", "application/word");
- cmd.Parameters.AddWithValue("@Data", size);
- cmd.Connection = con;
- con.Open();
- cmd.ExecuteNonQuery();
- con.Close();
- BindGridviewData();
- }
- }
- }
- else
- {
- ScriptManager.RegisterStartupScript(this, GetType(), "displayalertmessage", "myfunction();", true);
- }
- }
-
-
- protected void lnkDownload_Click(object sender, EventArgs e)
- {
- LinkButton lnkbtn = sender as LinkButton;
- GridViewRow gvrow = lnkbtn.NamingContainer as GridViewRow;
- int fileid = Convert.ToInt32(gvDetails.DataKeys[gvrow.RowIndex].Value.ToString());
- string name, type;
- using (SqlConnection con = new SqlConnection(strCon))
- {
- using (SqlCommand cmd = new SqlCommand())
- {
- cmd.CommandText = "select FileName, FileType, FileData from FileInformation where Id=@Id";
- cmd.Parameters.AddWithValue("@id", fileid);
- cmd.Connection = con;
- con.Open();
- SqlDataReader dr = cmd.ExecuteReader();
- if (dr.Read())
- {
- Response.ContentType = dr["FileType"].ToString();
- Response.AddHeader("Content-Disposition",
- "attachment;filename=\"" + dr["FileName"] + "\"");
- Response.BinaryWrite((byte[])dr["FileData"]);
- Response.End();
- }
- }
- }
- }
- }