Example Upload File - ASP.NET

This is example of upload file using asp.net.

Html 

 <fieldset style="width: 804px" align="center">
        <legend><em>Using the FileUpload Control</em>&nbsp;</legend>
        <div align="left" style="text-align: center">
            <form id="form1" runat="server">
                <div>
                    <table style="width: 90%">
                        <tr>
                            <td colspan="2">
                                <br />
                                <asp:Label ID="LSuccessMssg" runat="server"></asp:Label>
                                <asp:FileUpload ID="FileUpload1" runat="server" /><asp:Button ID="buttonUpload" runat="server"
                                    Text="Upload" OnClick="buttonUpload_Click1" /></td>
                        </tr>
                    </table>
                </div>
            </form>
        </div>
    </fieldset>

Code Behind

 protected void UploadThisFile(FileUpload upload)
    {
        if (upload.HasFile)
        {
            string theFileName = Path.Combine(Server.MapPath("~/Uploads"), upload.FileName);
            if (File.Exists(theFileName))
            {
                File.Delete(theFileName);
            }
            upload.SaveAs(theFileName);
        }
    }  

    protected void buttonUpload_Click1(object sender, EventArgs e)
    {
        UploadThisFile(FileUpload1);
        LSuccessMssg.Text = "Successfully upload File";
    }


By
NOTE : – If You have Found this post Helpful, I will appreciate if you can Share it on Facebook, Twitter and Other Social Media Sites. Thanks =)

Popular posts from this blog

Example to disable save as certain file type in SSRS Report Viewer

How to create DataGrid or GridView in JSP - Servlet

Control Webpart Visible/Enable using macro in Kentico