FileUpload in Asp.Net with Details



Here I have another Example for upload File in dot net. You can upload file and Check File Size, Name, File type and Status. Just Create Folder in Server and save uploaded File in Server Folder. I have created many post for download from server folder.

CODE:

<div>
        <fieldset style="width: 400px; margin-left: 240px; margin-top: 90px;">
            <legend>Example to Upload Multiple Files or images</legend>
            <asp:FileUpload ID="FileUpload1" runat="server" class="multi" />
            <br />
         <asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click" />
            <asp:Label ID="lblStatus" runat="server"></asp:Label>
        </fieldset>
    </div>

CS FILE:

protected void btnUpload_Click(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            try
            {
                HttpFileCollection hfc = Request.Files;
                for (int i = 0; i < hfc.Count; i++)
                {
                    HttpPostedFile hpf = hfc[i];
                    if (hpf.ContentLength > 0)
                    {
                        hpf.SaveAs(Server.MapPath("MyFiles") + "\\" + System.IO.Path.GetFileName(hpf.FileName));
                        sb.Append(String.Format("<br/><b>File Name: </b> {0}  <b>File Size:</b> {1}  <b>File Type:</b>  {2} uploaded successfully", hpf.FileName, hpf.ContentLength, hpf.ContentType));
                    }
                }
                lblStatus.ForeColor = Color.Green;
                lblStatus.Text = sb.ToString();
            }
            catch (Exception ex)
            {
                lblStatus.ForeColor = Color.Red;
                lblStatus.Text = "Error occured: " + ex.Message.ToString();
            }
        }
Check in browser and Upload file. 

Related Posts

Previous
Next Post »

Thanks for comments.....