Upload File without clicking Submit Button using FileUpload control in ASP.Net with c#

Many times we want to upload image with click on save button using this article we have upload image without click any button


Source Code :-

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

    <script type="text/javascript">
        function UploadFile(fileUpload) {
            if (fileUpload.value != '') {
                document.getElementById("<%=btnUpload.ClientID %>").click();
        }
    }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:FileUpload ID="FileUpload1" runat="server" />
            <br />
            <asp:Label ID="lblmsg" runat="server" Text="File uploaded successfully." ForeColor="Green"
                Visible="false" />
            <asp:Button ID="btnUpload" Text="Upload" runat="server" OnClick="Upload" Style="display: none" />
        </div>

    </form>
</body>
</html>

Code behind(c#):-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public partial class fileuploaderuploadwithoutclick : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        FileUpload1.Attributes["onchange"] = "UploadFile(this)";
    }

    protected void Upload(object sender, EventArgs e)
    {
        FileUpload1.SaveAs(Server.MapPath("~/images/" + Path.GetFileName(FileUpload1.FileName)));
        // you can use here datavase also and save the values in database

        lblmsg.Visible = true;
    }
}

Note:- We need to check that we have created a folder or not if then must be created.

Out-Put:-


Upload File without clicking Submit Button using FileUpload control in ASP.Net with c# Upload File without clicking Submit Button using FileUpload control in ASP.Net with c# Reviewed by NEERAJ SRIVASTAVA on 1:22:00 PM Rating: 5

No comments:

Powered by Blogger.