How to File Upload asp.net With C#


The FileUpload and File will be uploaded using FileUpload control and will be inserted into SQL Server Database Table. You can use FileUpload Control asp.net  with c#

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="fileupload.aspx.cs" Inherits="fileupload" %>

    <!DOCTYPE html>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <asp:FileUpload id="FileUploadControl" runat="server" />
        <asp:Button runat="server" id="btnupload" text="Upload" OnClick="btnupload_Click" />
        <br /><br />
        <asp:Label runat="server" id="lblsms" text="Upload status: " />
    </form>
    </body>
    </html>

Afterward, click the upload button and write the code, given below:
Source Code C# ...

  1. using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public partial class fileupload : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void btnupload_Click(object sender, EventArgs e)
        {
            if (FileUploadControl.HasFile)
            {
                try
                {
                    if (FileUploadControl.PostedFile.ContentType == "image/jpeg")
                    {
                        if (FileUploadControl.PostedFile.ContentLength < 102400)
                        {
                            string filename = Path.GetFileName(FileUploadControl.FileName);
                            FileUploadControl.SaveAs(Server.MapPath("~/") + filename);
                            StatusLabel.Text = "Upload status: File uploaded!";
                        }
                        else
                            lblsms.Text = "Upload status: The file has to be less than 100 kb!";
                    }
                    else
                        lblsms.Text = "Upload status: Only JPEG files are accepted!";
                }
                catch (Exception ex)
                {
                    lblsms.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
                }
            }
        }
    }

Post a Comment

1 Comments

  1. You are searching jobs. then You can find many jobs in jobdoin.com

    sarkari result

    ReplyDelete