asp.net.ph

Skip Navigation Links

FileUpload Control Syntax

ASP.NET Syntax   ASP.NET Syntax for Web Controls


Creates an <input type=file> control ( usually shown as a text box control and a browse button ) that enables users to select a file to upload to the server.

Declarative Syntax

For information on the individual members of this class, see FileUpload in the class library.

Remarks

The FileUpload control displays a text box control and a browse button that allows users to select a file on the client and upload it to the Web server.

The user specifies the file to upload by entering the full path to the file on the local computer (for example, C:\MyFiles\TestFile.txt) in the text box of the control. Alternatively, the user can select the file by clicking the Browse button, and then locating it in the Choose File dialog box.

The FileUpload control does not automatically send a file to the server after the user selects the file to upload. You must explicitly provide a control or mechanism to allow the user to submit the form. Typically, the file is saved or the contents handled in an event-handling method for an event that raises a post back to the server.

For example, if you provide a button to submit a file, you could place the code to save the file inside the event-handling method for the click event.

Syntax Example

The following example demonstrates how to create a FileUpload control that saves files to a path that is specified in code. The SaveAs method is called to save the file to the specified path on the server.

The ASP.NET application that includes the example must have Write access to the specified directory on the server. There are basically two ways to do this:

  • explicitly grant Write access to the account under which the application is running, in the directory in which the uploaded files will be saved, or
  • increase the level of trust that is granted to the ASP.NET application.

Note that this example does not perform all the necessary error checking. If a file with the same name already exists in the specified path, the uploaded file overwrites it.

<script language="C#" runat="server">
   protected void UploadButton_Click ( object sender, EventArgs e ) {
      // specify the path on the server to save the uploaded file to.
      String savePath = @"c:\tmp\";

      // before attempting to perform operations on the file, 
      // verify that the FileUpload control contains a file
      if ( fileUploader.HasFile ) {
         // get the name of the file to upload.
         String fileName = fileUploader.FileName;

         // append the name of the file to upload to the path.
         savePath += fileName;

         // call the SaveAs method to save the uploaded file to the specified path.
         fileUploader.SaveAs ( savePath );

         // notify the user of the name of the file was saved under.
         uploadStatusLabel.Text = "Your file was saved as " + fileName;
      } else {
         // notify the user that a filename is needed
         uploadStatusLabel.Text = "You did not specify a file to upload.";
      }
   }
</script>

<form runat="server">

<p id="msg">Select a file to upload:</p>

<p><asp:fileupload id="fileUploader" runat="server" />

<asp:button id="UploadButton"
   text = "Upload file"
   onclick = "UploadButton_Click"
   runat="server" />
</p>

<p><asp:label id="uploadStatusLabel" runat="server" />
</p>

</form>
  C# VB

 Show me 

See Also

FileUpload Class   FileUpload Web Server Control



© 2025 Reynald Nuñez and asp.net.ph. All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note