How can users upload files using my Web Forms application?
To upload a file from a client to your Web server, you can use the File Field HTML control in conjunction with the System.Web.UI.HtmlControls.HtmlInputFile class.
Creating A Web Form
- Add a New Web Form to the project (you can also use an existing form)
- At the bottom left of the form, select the HTML tab. Set the enctype attribute as follows:
<form id="UploadForm" method="post" runat="server" enctype="multipart/form-data">
Adding a File Field HTML Control to the Web Form
- Drag and drop a File Field control on the Web Form (you can resize it after you drop it). It should look something like this:
- Right-click the File Field control and select Run As Server Control from the shortcut menu.
- With the File Field control selected, go to the Properties Window and change the (id) property to FileSelect.
- Select the Web Forms tab in the Visual Studio Toolbox, then drag and drop a Label onto the form.
- With the label selected, go to the Properties Window and set the (ID) property to lblResult, and the Visible property to False. This label will be used to display the results of the upload.
Adding an Upload Button to the Web Form
- Drag and drop a Submit Button from the Visual Studio Toolbox onto the Web Form.
- In the Properties Window, set the Value field to Upload. It should look something like this:
- Right-click the Submit button and select Run As Server Control from the shortcut menu.
- With the Submit button selected, go the Properties Window and change the (id) and name properties to btnUpload.
- Double-click the Submit button. This adds a btnUpload_ServerClick event handler method to the Web Form's code-behind file.
- Add the following code to this method (change the drive/path to whatever you want):
In C:
private void btnUpload_ServerClick(object sender, System.EventArgs e) { try { // Get the file name without a path string FileName = System.IO.Path.GetFileName(this.FileSelect.PostedFile.FileName); // Save the file in the temp directory FileSelect.PostedFile.SaveAs("c:\\temp\\" + FileName); // Display success! lblResult.Text = "File successfully uploaded"; } catch { // Display unsuccessful lblResult.Text = "Error uploading...please check file"; } lblResult.Visible = true; }In VB .NET:
Private Sub btnUpload_ServerClick(sender As Object, e As System.EventArgs) Try ' Get the file name without a path Dim FileName As String = _ System.IO.Path.GetFileName( _ Me.FileSelect.PostedFile.FileName) ' Save the file in the temp directory FileSelect.PostedFile.SaveAs(("c:\temp\" + FileName)) ' Display success! lblResult.Text = "File successfully uploaded" Catch ' Display unsuccessful lblResult.Visible = True End Try End Sub
After completing these steps you're ready to run your upload form.
Note: You can show other pertinent information about the uploaded file by displaying the value of other FileSelect.PostedFile properties such as:
- ContentLength (size in bytes)
- ContentType (the file MIME type)
© (c) 2026 Oak Leaf Enterprises, Inc., 1996-2026 • Updated: 04/27/18
Comment or report problem with topic
