Search This Blog

Saturday, September 7, 2013

Upload/Download File - Asp.net


Click  On Choose File To Upload File .

And

Click On Link Below Download To Download File From Your Project Page.

Create One Directory Name "file" in Your Project
And Create Table That Stores the id And path of your file.

Two source code files
Coding in Aspx Design of webpage and
Code .cs file contains C# coding
Aspx code

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:FileUpload ID="FileUpload1" runat="server" />
        <asp:Button ID="Submit" runat="server"
            Text="Submit" onclick="Submit_Click" />
        <br />
        <br />
        <br />
        <br />
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
            <Columns>
                <asp:TemplateField HeaderText="Download">
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkButton1" runat="server"
                            CommandArgument='<%# bind("path") %>' onclick="LinkButton1_Click"
                            Text='<%# bind("path") %>'></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>






aspx.cs code 

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;

using System.IO;
public partial class _Default : System.Web.UI.Page
{
 
    SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\WebSite1\WebSite14\App_Data\Database.mdf;Integrated Security=True;User Instance=True");
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            showdata();
        }
    }

    private void showdata()
    {
        SqlDataAdapter da = new SqlDataAdapter("select * from tblfile",con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
    protected void Submit_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            string fname = FileUpload1.FileName;
            string fpath = "~\\file\\"+fname;
            FileUpload1.SaveAs(Server.MapPath(fpath));
            SqlCommand cmd = new SqlCommand("insert into tblfile values('"+fpath+"')",con);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        string filePath = (sender as LinkButton).CommandArgument;
        Response.ContentType = ContentType;
        Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath));
        Response.WriteFile(filePath);
        Response.End();
    }
}
For Learn C#.Net, ASP.Net, VB.Net Visit OurWebsite

No comments:

Post a Comment