File Upload Control without using upload button

File Upload Control without using upload button

code of page load event

if (!IsPostBack)
        {

            FileUpload1.Attributes["onchange"] = "UploadFile(this)";
        }

Create Upload Function


protected void Upload(object sender, EventArgs e)
    {
        FileUpload1.SaveAs(Server.MapPath(@"~/Admin/") + "//member//" + FileUpload1.FileName);
         Label1.Text = FileUpload1.FileName;
        Image1.ImageUrl = "/Admin/member/" + Label1.Text;
     
    }

Code inside Aspx

<HEAD>
  <script type="text/javascript">

        function UploadFile(fileUpload)
        {
            if (fileUpload.value != '')
            {
                document.getElementById("<%=btnUpload.ClientID %>").click();
            }
        }
</script>
</HEAD>

Set File upload property

 <asp:FileUpload ID="FileUpload1" runat="server"/><br />

<asp:Button ID="btnUpload" Text="Upload" runat="server" OnClick="Upload" Style="display: none" />

Popular Posts