ASP DOT NET

Saturday, June 9, 2018

Q.How to upload and download files using asp.net c#


.aspx

 <form id="form1" runat="server">
   <div style="width: 400px">
            <div style="clear: both; width: 100%">
                 <asp:Label ID="lblMessage" runat="server" Text="" Font-Names="Arial"></asp:Label>
                <br />
               <asp:FileUpload ID="FileUpload1" runat="server" />
                <asp:Button ID="btnUpload" Text="Upload File" runat="server" OnClick="btnUpload_Click"/>
            </div>
            <div style="margin-top: 5px; clear: both">
                <asp:GridView ID="gvFiles" CssClass="GridViewStyle" AutoGenerateColumns="true" runat="server">
                    <FooterStyle CssClass="GridViewFooterStyle" />
                    <RowStyle CssClass="GridViewRowStyle" />
                    <SelectedRowStyle CssClass="GridViewSelectedRowStyle" />
                    <PagerStyle CssClass="GridViewPagerStyle" />
                    <AlternatingRowStyle CssClass="GridViewAlternatingRowStyle" />
                    <HeaderStyle CssClass="GridViewHeaderStyle" />
                    <Columns>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:HyperLink runat="server" NavigateUrl='<%# Eval("ID", "GetFile.aspx?ID={0}") %>' Text="Download"></asp:HyperLink>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
            </div>
        </div>
    </form>

aspx.cs

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using UploadAndDownload.Models;

namespace UploadAndDownload
{
    public partial class UploadandDownload : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataTable fileList = FileUtilities.GetFileList();
                gvFiles.DataSource = fileList;
                gvFiles.DataBind();
            }
        }
        protected void btnUpload_Click(object sender, EventArgs e)
        {
            string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
            string contentType = FileUpload1.PostedFile.ContentType;
            using (Stream fs = FileUpload1.PostedFile.InputStream)
            {
                using (BinaryReader br = new BinaryReader(fs))
                {
                    byte[] bytes = br.ReadBytes((Int32)fs.Length);
                    string constr = ConfigurationManager.ConnectionStrings["PnG_AQUAConnection"].ConnectionString;
                    using (SqlConnection con = new SqlConnection(constr))
                    {
                        string query = "insert into Files(Name,ContentType,Size,Data) values(@Name, @ContentType,@Size, @Data)";
                        using (SqlCommand cmd = new SqlCommand(query))
                        {
                            cmd.Connection = con;
                            cmd.Parameters.AddWithValue("@Name", filename);
                            cmd.Parameters.AddWithValue("@ContentType", contentType);
                            cmd.Parameters.AddWithValue("@Size", bytes);
                            cmd.Parameters.AddWithValue("@Data", bytes);
                            con.Open();
                            int result = cmd.ExecuteNonQuery();
                            if (result > 0)
                            {
                                //Response.Write("<script>alert('Your file uploaded successfully')</script>");
                                ScriptManager.RegisterStartupScript(this, this.GetType(), "alert()", "alert('Your file uploaded successfully')", true);
                            }
                            con.Close();
                        }
                    }
                }
            }
        }
    }
}
 Table.

OutPut.










No comments:

Post a Comment

How to to select duplicate rows from sql server?

 SELECT * FROM Recruitment WHERE Email IN (SELECT Email FROM Recruitment GROUP BY Email HAVING COUNT(*) > 1); WITH CTE AS (     SELECT   ...