Export (0) Print
Expand All

HtmlInputFile Control

Creates a server-side control that maps to the <input type=file> HTML element and allows you to upload a file to the server.

<input type=file
       id="programmaticID"
       accept="MIMEencodings"
       maxlength="maxfilepathlength"
       size="widthoffilepathtextbox"
       postedfile="uploadedfile"
       runat="server" >

Remarks

Use the HtmlInputFile control to program against the HTML <input type=file> element. You can use the HtmlInputFile control to easily design a page that allows users to upload binary or text files from a browser to a directory that you designate on your Web server. File upload is enabled in all HTML 3.2 and later Web browsers.

Example

The following example demonstrates a simple file upload scenario. The first section of code defines the event handler for the page. When the user clicks the Upload button on the form, the file name, content length, and amount of content (in bytes), are displayed on the page, while the file itself is uploaded to the UploadedFiles directory on the server.

Note   You must set the enctype attribute of the form to "multipart/form-data".

The code for the form implements an HtmlForm control, an HtmlInputFile control, an HtmlInputButton control, and four HtmlGenericControls (the <div> element and the three <span> elements, each with runat="server" attribute/value pairs in their opening tags).

Note   To view the information about the uploaded file on the page, the Visible property, which the HtmlGenericControl inherits from the Control class, must be set to true in the event-handler code.
<%@ Page Language="VB" AutoEventWireup="True" %>
<html>
   <script runat="server">
      Sub UploadBtn_Click(Sender as Object, e as EventArgs)

         ' Display information about posted file
         FileName.InnerHtml = MyFile.PostedFile.FileName
         MyContentType.InnerHtml = MyFile.PostedFile.ContentType 
         ContentLength.InnerHtml = cStr(MyFile.PostedFile.ContentLength)
         FileDetails.Visible = True

         ' Save uploaded file to server
         MyFile.PostedFile.SaveAs("c:\Uploadedfiles\uploadfile.txt")
      End Sub
   </script>
   <body>
      <form action="fileupload.aspx" 
            method="post"
            enctype="multipart/form-data" 
            runat="server">

         <h1>ASP.NET File Upload Example</h1>
         Select File To Upload to Server: 
         <input id="MyFile" 
                type="file" 
                runat="server"> 
         <br><br>
         <input type=submit 
                value="Upload!"
                OnServerclick="UploadBtn_Click" 
                runat="server">
         <br><br><br>
         <div id="FileDetails" 
              Visible=false 
              runat="server">
            FileName: <span id="FileName" runat="server"/> <br>
            ContentType: <span id="MyContentType" runat="server"/> <br>
            ContentLength: <span id="ContentLength" runat="server"/>bytes
            <br>
         </div>
      </form>
   </body>
</html>
[C#]
<%@ Page Language="C#" AutoEventWireup="True" %>
<html>
   <script runat="server">
      void UploadBtn_Click(Object sender, EventArgs e)
      {
         // Display information about posted file
         FileName.InnerHtml = MyFile.PostedFile.FileName;
         MyContentType.InnerHtml = MyFile.PostedFile.ContentType; 
         ContentLength.InnerHtml =
                               MyFile.PostedFile.ContentLength.ToString();
         FileDetails.Visible = true;

         // Save uploaded file to server
         MyFile.PostedFile.SaveAs("c:\\Uploadedfiles\\uploadfile.txt");
      }
   </script>
   <body>
      <form action="fileupload.aspx" 
            method="post"
            enctype="multipart/form-data" 
            runat="server">

         <h1>ASP.NET File Upload Example</h1>
         Select File To Upload to Server: 
         <input id="MyFile" 
                type="file" 
                runat="server"> 
         <br><br>
         <input type=submit 
                value="Upload!"
                OnServerclick="UploadBtn_Click" 
                runat="server">
         <br><br><br>
         <div id="FileDetails" 
              Visible=false 
              runat="server">
            FileName: <span id="FileName" runat="server"/> <br>
            ContentType: <span id="MyContentType" runat="server"/> <br>
            ContentLength: <span id="ContentLength" runat="server"/>bytes
            <br>
         </div>
      </form>
   </body>
</html>

See Also

ASP.NET Syntax for HTML Controls | HtmlInputFile Class

Show:
© 2014 Microsoft