asp.net.ph

Skip Navigation LinksHome > Getting Started > ASP.NET Syntax > ASP.NET Syntax for HTML Controls > HtmlInputFile

HtmlInputFile Control Syntax

HtmlInputFile Class   Input File Element


Creates a file upload control.

Declarative Syntax

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

Syntax Notes

  • This control does not require an end tag.

Working with HtmlInputFile

The HtmlInputFile control enables programming of the HTML <input type=file> element.

Developers can use the HtmlInputFile control to easily design a page that allows users to upload a file to a designated directory on a Web server.

Creating a Form That Uploads a File to the Server

The following example shows a simple file upload scenario.

When the user clicks the Upload button on the form, the file name, content type, and the amount of content in bytes are displayed on the page, while the file itself is uploaded to a specified directory on the server.

Creating a Form That Uploads a File to the Server
Run Sample | View Source

NOTE: When using this control, the Enctype property of the containing HtmlForm must be set to "multipart/form-data".

  1. The code for the form implements an HtmlForm control, an HtmlInputFile control, an HtmlInputText control, an HtmlInputButton control, and an HtmlGenericControl ( the Div element ).
    <form enctype="multipart/form-data" runat="server">
    
    <p>File to Upload:
       <input id="fileToUpload" type=file runat="server">
    
    <p>Save as filename ( no path ) :
       <input id="fileName" type=text runat="server">
    
    <p><input type=button runat="server" id="Button1" 
       value="Upload" onServerClick = "uploadFile">
    
    <p><div id="details" runat="server" visible=false
       style = "background:#def; border:1px inset" />
    
    </form>
  2. In the <head> of the Web Forms page, define the handler for the file upload button’s Click event.
<script language="C#" runat="server">
void uploadFile ( object Source, EventArgs e ) {
   if ( fileName.Value == "" ) {
      details.InnerHtml = "You must enter a file name";
      return;
   }

   if ( fileToUpload.PostedFile != null ) {
      try {
         fileToUpload.PostedFile.SaveAs ( "c:\\temp\\" + fileName.Value );

         string html = "<p>File uploaded successfully on the web server</p>";
         html += "<p>FileName: c:\\temp\\" + fileName.Value + "<br>";
         html += "ContentType: " + fileToUpload.PostedFile.ContentType + "<br>";
         html += "ContentLength: "+ fileToUpload.PostedFile.ContentLength + "</p>";

         details.InnerHtml = html;
      }
      catch ( Exception exc ) {
         details.InnerHtml = "Error saving file <b>c:\\temp\\" + 
            fileName.Value+"</b><br>" + exc.ToString ( );
      }
      finally {
         details.Visible = true;
      }
   }
}
</script>
See Also

HtmlInputFile Class   Input File Element



© 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