Skip to main content

custom File Upload ASP.net C#



Default.aspx.cs



PageLolad 

 protected void Page_Load(object sender, EventArgs e)

    {

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

    }




protected void Upload(object sender, EventArgs e)

    {

        FileUpload1.SaveAs(Server.MapPath("~/AAA/Uploads/" +                                               Path.GetFileName(FileUpload1.FileName)));       

        lblMessage.Visible = true;

    }

---------------------------------------------------------------------------------------------------------------



Default.aspx



inside  form tag  put this code


        <asp:FileUpload ID="FileUpload1" accept=".png,.jpg,.jpeg,.gif" Style="display: none" runat="server" />

            <label for="FileUpload1">

                <img src="downloa.png" />

            </label>

<br />

<asp:Label ID="lblMessage" runat="server" Text="File uploaded successfully." ForeColor="Green"

    Visible="false" />




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




        <script type="text/javascript">

            function UploadFile(fileUpload) {

                if (fileUpload.value != '') {

                    document.getElementById("<%=btnUpload.ClientID %>").click();

        }

    }

</script>


Auto upload  image like facebook and custom image uploader


classic Upload Asp.net

classic file upload





Custom Upload file





Comments

Popular posts from this blog

WebForms UnobtrusiveValidationMode requires a ScriptResourceMapping for 'jquery'. Please add a ScriptResourceMapping named jquery(case-sensitive).

  WebForms UnobtrusiveValidationMode requires a ScriptResourceMapping for 'jquery'. Please add a ScriptResourceMapping named jquery(case-sensitive). Solution   < appSettings > < add key = "ValidationSettings:UnobtrusiveValidationMode" value = "None" /> </ appSettings >

Delete put code

   <system.webServer>     <validation validateIntegratedModeConfiguration="false"/>     <modules runAllManagedModulesForAllRequests="true">         <remove name="WebDAVModule"/> <!-- ADD THIS -->     </modules>      <handlers>            <remove name="ExtensionlessUrlHandler-Integrated-4.0" />       <remove name="OPTIONSVerbHandler" />       <remove name="TRACEVerbHandler" />       <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />     </handlers></system.webServer>