In this article I will explain with an example, how to upload File using the HTML INPUT File element (<input type="file" />) for uploading files in ASP.Net using C# and VB.Net.
 
 
HTML Markup
The following HTML Markup consists of an HTML INPUT File element, ASP.Net Button control and Label control.
Note: In order to upload file using the HTML INPUT File element, you will need to set enctype attribute in the Form with value multipart/form-data.
 
<form id="form1" runat="server" enctype="multipart/form-data">
    <input type="file" name="FileUpload" />
    <asp:Button ID="Button1" Text="Upload" runat="server" OnClick="Upload" />
    <br />
    <asp:Label ID = "lblMessage" Text="File uploaded successfully." runat="server" ForeColor="Green" Visible="false" />
</form>
 
 
Namespaces
You will need to import the following namespace.
C#
using System.IO;
 
VB.Net
Imports System.IO
 
 
Upload file using HTML INPUT File in ASP.Net
When the Upload button is clicked, the uploaded file is fetched from the Request.Files collections using the Name of the HTML INPUT File element.
If the value of the File is not NULL then it is saved into the respective folder and the success message is displayed to the User.
C#
protected void Upload(object sender, EventArgs e)
{
    //Access the File using the Name of HTML INPUT File.
    HttpPostedFile postedFile = Request.Files["FileUpload"];
 
    //Check if File is available.
    if (postedFile != null && postedFile.ContentLength > 0)
    {
        //Save the File.
        string filePath = Server.MapPath("~/Uploads/") + Path.GetFileName(postedFile.FileName);
        postedFile.SaveAs(filePath);
        lblMessage.Visible = true;
    }
}
 
VB.Net
Protected Sub Upload(ByVal sender As Object, ByVal e As EventArgs)
    'Access the File using the Name of HTML INPUT File.
    Dim postedFile As HttpPostedFile = Request.Files("FileUpload")
    'Check if File is available.
    If postedFile IsNot Nothing And postedFile.ContentLength > 0 Then
        'Save the File.
        Dim filePath As String = Server.MapPath("~/Uploads/") + Path.GetFileName(postedFile.FileName)
        postedFile.SaveAs(filePath)
        lblMessage.Visible = True
    End If
End Sub
 
 
Screenshot
Upload file using HTML INPUT File (<input type=”file” />) in ASP.Net
 
 
Downloads