In this article I will explain a Tutorial with example, how to use the ASP.Net FileUpload control in C# and VB.Net.
The ASP.Net FileUpload control is supported in .Net 2.0, 3.0, 3.5, 4.0 and 4.5 versions.
 
 
HTML Markup
The following HTML Markup consists of an ASP.Net FileUpload control, a Button and a Label control. The Button has been assigned a Click event handler.
<asp:FileUpload ID="FileUpload1" runat="server" />
<hr />
<asp:Button ID="btnUpload" Text="Upload" runat="server" OnClick="UploadFile" />
<br />
<asp:Label ID="lblMessage" ForeColor="Green" runat="server" />
 
 
Namespaces
You will need to import the following namespace.
C#
using System.IO;
 
VB.Net
Imports System.IO
 
 
Create Folder (Directory) and Upload file in ASP.
When the Upload Button is clicked, first a check is performed whether the Folder (Directory) exists. If it does not then the Folder (Directory) is created.
Then the uploaded File is saved into the Folder (Directory).
Finally a success message is displayed on the screen using the Label control.
C#
protected void UploadFile(object sender, EventArgs e)
{
    string folderPath = Server.MapPath("~/Files/");
       
    //Check whether Directory (Folder) exists.
    if (!Directory.Exists(folderPath))
    {
        //If Directory (Folder) does not exists. Create it.
        Directory.CreateDirectory(folderPath);
    }
 
    //Save the File to the Directory (Folder).
    FileUpload1.SaveAs(folderPath + Path.GetFileName(FileUpload1.FileName));
 
    //Display the success message.
    lblMessage.Text = Path.GetFileName(FileUpload1.FileName) + " has been uploaded.";
}
 
VB.Net
Protected Sub UploadFile(sender As Object, e As EventArgs)
    Dim folderPath As String = Server.MapPath("~/Files/")
 
    'Check whether Directory (Folder) exists.
    If Not Directory.Exists(folderPath) Then
        'If Directory (Folder) does not exists. Create it.
        Directory.CreateDirectory(folderPath)
    End If
 
    'Save the File to the Directory (Folder).
    FileUpload1.SaveAs(folderPath & Path.GetFileName(FileUpload1.FileName))
 
    'Display the success message.
    lblMessage.Text = Path.GetFileName(FileUpload1.FileName) + " has been uploaded."
End Sub
 
 
Screenshot
ASP.Net FileUpload control Tutorial with example in C# and VB.Net
 
 
Downloads