My Account
Home Products Support Company Contact Buy

Retrieve File Name of an Uploaded File


It is often desirable to get the file name from a File Upload control. For example, you might want to store the original file name of an uploaded file in your database record along with the actual file.
- Pooja Daga, Technical Support Engineer, Iron Speed, Inc.

April 26, 2006
Iron Speed Designer V3.2

Introduction

It is often desirable to get the file name from a File Upload control. For example, you might want to store the original file name of an uploaded file in your database record along with the actual file.

Using an ASP.NET Control

Step 1: To include a File Upload control in a page called MyPage.aspx, add the following line to the HTML layout page file for this page.

<INPUT type=file id=inputFile name=inputFile runat="server" />

For .NET Framework 1.1, add the code below in the “MyPage” class of MyPage.aspx.cs

C#:

protected System.Web.UI.HtmlControls.HtmlInputFile inputFile;

Visual Basic .NET:

Protected inputFile As System.Web.UI.HtmlControls.HtmlInputFile

These definitions are not needed for .NET Framework 2.0. Note that “inputFile” is the id of the File control.

Step 2: Retrieve the file name of the uploaded file using the code below. Add this code in the click event of your button control as shown below.

C#:

public MyPage()
{
    this.Init += new System.EventHandler(my_init);
}
public void my_init(object sender, System.EventArgs e)
{
    this.Button.Button.Click += new System.EventHandler(onButtonClick);
}
private void onButtonClick(object sender, System.EventArgs e)
{
    string fileName = System.IO.Path.GetFileName(inputFile.PostedFile.FileName);
}

Visual Basic .NET:

Private Sub Page_Init(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles MyBase.Init
    AddHandler Button.Button.Click, AddressOf Me.OnButton_Click
End Sub
 
private Sub OnButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Dim fileName As String = System.IO.Path.GetFileName(inputFile.PostedFile.FileName)
End Sub

Step 3: Build and run your application.

Using an Iron Speed Designer "File Upload" Control

Alternatively, you can use a File Upload control from the tool box in Iron Speed Designer.

Step 1: Drag and drop a File Upload control from the tool box in Iron Speed Designer. Bind the control by double clicking on the File Upload control and go to the Bindings tab in the Page Properties dialog.

Step 2: Drag and drop a Button control from the tool box in Iron Speed Designer. Double click the Button control to display Page Properties dialog and go to the Display tab. Enter the button text.

Step 3: Add the following button click event code in the Page class of your page to retrieve the file name of the uploaded file.

C#:

public MyPage()
{
    this.Init += new System.EventHandler(MyInit);
}
public void MyInit(object sender, System.EventArgs e)
{
    this.Button.Button.Click += new System.EventHandler(OnButton_Click);
}
private void OnButton_Click(object sender, System.EventArgs e)
{
    string fileName = this.FileUpload.File.FileName;
    fileName = fileName.Substring(fileName.LastIndexOf("\\") +1);
}

Visual Basic .NET:

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
    AddHandler Button.Button.Click, AddressOf Me.OnButton_Click
End Sub
 
Private Sub OnButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Dim fileName As String = Me.FileUpload.File.FileName
    fileName = fileName.Substring(fileName.LastIndexOf("\") + 1
End Sub

Note that FileUpload is the name of the File Upload control.

Step 4: Build and run your application.

About the Author

Pooja Daga
Technical Support Engineer, Iron Speed, Inc.

Pooja has experience developing Web applications using .NET technology. She started her career with PCS, a leading software services company headquartered in India and has been with Iron Speed since 2005.

Pooja holds a M.S. degree in Computer Application and a B.S. degree in Electrical engineering from Maharaja Sayajirao University in India.



  Privacy Statement