How to File Upload 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  with c#

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

    <!DOCTYPE html>

    <html xmlns="">
    <head runat="server">
        <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: " />

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)
                    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!";
                            lblsms.Text = "Upload status: The file has to be less than 100 kb!";
                        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. You are searching jobs. then You can find many jobs in

    sarkari result