Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

Fileupload control in asp.net - Part 30

Suggested Videos
Part 19 - Mapping virtual path to physical path using Server.MapPath method
Part 20 - Mapping virtual path to physical path using Server.MapPath method Example

In this video we will discuss about fileupload control. FileUpload control is a combination of a text box and a browse button that enable users to select a file to upload to the server.

Create an asp.net web application project. Drag and drop the FileUpload control on the webform.



The fileUpload control only allows the user to select the file. To upload the seleceted file, drag and drop a button control. Change the ID of the button to btnUpload and the Text to Upload File. Also drag and drop a label control, and change the ID of the label to lblMessage. At this stage the HTML of the webform should be as shown below.
<asp:FileUpload ID="FileUpload1" runat="server" />
&nbsp;
<asp:Button ID="btnUpload" runat="server" Text="Upload File" 
    onclick="btnUpload_Click" />
<br />
<asp:Label ID="lblMessage" Font-Bold="true" runat="server">
</asp:Label>



Right click on the web application project and add a folder with name Uploads. This folder is going to store all the uploaded files. 

Copy and paste the following code in btnUpload_Click() event handler
// If the user has selected a file
if (FileUpload1.HasFile)
{
    // Get the file extension
    string fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName);

    if (fileExtension.ToLower() != ".doc" && fileExtension.ToUpper() != ".docx")
    {
        lblMessage.ForeColor = System.Drawing.Color.Red;
        lblMessage.Text = "Only files with .doc and .docx extension are allowed";
    }
    else
    {
        // Get the file size
        int fileSize = FileUpload1.PostedFile.ContentLength;
        // If file size is greater than 2 MB
        if (fileSize > 2097152)
        {
            lblMessage.ForeColor = System.Drawing.Color.Red;
            lblMessage.Text = "File size cannot be greater than 2 MB";
        }
        else
        {
            // Upload the file
            FileUpload1.SaveAs(Server.MapPath("~/Uploads/" + FileUpload1.FileName));
            lblMessage.ForeColor = System.Drawing.Color.Green;
            lblMessage.Text = "File uploaded successfully";
        }
    }
}
else
{
    lblMessage.ForeColor = System.Drawing.Color.Red;
    lblMessage.Text = "Please select a file";
}

10 comments:

  1. Hello Venkat,
    Thanks for the tutorials. How can the code be modified so as to save the path of the file uploaded to a database?

    ReplyDelete
  2. Thanks for the tutorials. How can the code be modified so as to save the path of the file uploaded to a database?

    ReplyDelete
  3. how to rename image with id.plz its urgent

    ReplyDelete
  4. I think you want to write
    if (fileExtension.ToLower() != ".doc" && fileExtension.ToLower() != ".docx")

    Instead of
    if (fileExtension.ToLower() != ".doc" && fileExtension.ToUpper() != ".docx")

    ReplyDelete
    Replies
    1. .docx is next version of doc .

      Delete
    2. @Tiago

      You Right need to convert it to lower not upper

      or use upper and write it like ".DOCX"

      Delete
  5. sir how can i overrite the existing file while uploading a new file

    ReplyDelete
  6. Hello sir,
    how can i upload image in running mode on a web page.
    Your all tutorials are really excellent.

    ReplyDelete
  7. LOVE your tutorials! I have one question, I'd like to change the upload size to 6 MB, so I changed it to below, but instead of getting the nice red error I get an ugly exception. Ideas?

    // Get the file size

    int fileSize = FileUpload1.PostedFile.ContentLength;
    // If file size is greater than 6 MB
    if (fileSize > 6291456)
    {
    lblMessage.ForeColor = System.Drawing.Color.Red;
    lblMessage.Text = "File size cannot be greater than 6 MB";
    }
    else
    {
    // Upload the file
    FileUpload1.SaveAs(Server.MapPath("~/Uploads/" + FileUpload1.FileName));
    lblMessage.ForeColor = System.Drawing.Color.Green;
    lblMessage.Text = "File uploaded successfully";
    }

    ReplyDelete
  8. if (fileExtension.ToLower() != ".doc" && fileExtension.ToUpper() != ".docx")
    here ToUpper has be changed to ToLower or .docx to .DOCX

    ReplyDelete

It would be great if you can help share these free resources