Export (0) Print
Expand All

HtmlInputFile Class

Allows programmatic access to the HTML <input type= file> element on the server.

Namespace:  System.Web.UI.HtmlControls
Assembly:  System.Web (in System.Web.dll)

[ValidationPropertyAttribute("Value")]
[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public class HtmlInputFile : HtmlInputControl, 
	IPostBackDataHandler
<asp:HtmlInputFile />

Use the HtmlInputFile server control to handle uploading binary or text files from a browser client to the server. File upload works with Microsoft Internet Explorer version 3.02 or later.

NoteNote:

The HtmlInputFile control is designed to be used only in postback scenarios and not in asynchronous postback scenarios during partial-page rendering. When you use a HtmlInputFile control inside an UpdatePanel control, the file must be uploaded by using a control that is a PostBackTrigger object for the panel. UpdatePanel controls are used to update selected regions of a page instead of updating the whole page with a postback. For more information, see UpdatePanel Control Overview and Partial-Page Rendering Overview.

To specify the file to upload, enter the full path to the file (for example, "C:\MyFiles\Test.txt") in the text box of the control. You can also select the file by clicking the Browse button and then locating it in the Choose file dialog box.

NoteNote:

The Enctype property of an HtmlForm control must be set to "multipart/form-data" for this control to work properly.

The HtmlInputFile control does not have a built-in way to post back to the server. To upload the file to the server, use the SaveAs method of the System.Web.HttpPostedFile object contained in the PostedFile property. This operation is usually done in an event-handling method, such as for a button click.

NoteNote:

When you use the SaveAs method of the HttpPostedFile class, make sure to specify a rooted path (for example, "C:\MyFiles\Test.txt"). The behavior of the SaveAs method can be specified in the httpRuntime Element (ASP.NET Settings Schema) configuration section with the requireRootedSaveAsPath attribute. The default value of this attribute is true. If you set this attribute to false, then using the SaveAs method with no path specified will attempt to place the file in the systemroot \system32\inetsrv directory. By default, this directory is write-protected; therefore, the file can not be saved there. Make sure to provide write permission for the account used by ASP.NET for the directory where you want to store the file.

When uploading large files, use the maxRequestLength attribute of the httpRuntime Element (ASP.NET Settings Schema) element to increase the maximum allowable file size. A DNS error is generated in the browser when the file exceeds the specified size. You might also receive the following error message when uploading large files:

aspnet_wp.exe (PID: 1520) was recycled because memory consumption exceeded 460 MB (60 percent of available RAM).

If you encounter this error message, increase the value of the memoryLimit attribute in the processModel Element (ASP.NET Settings Schema) element of the Machine.config file.

For a list of initial property values for an instance of HtmlInputFile, see the HtmlInputFile constructor.

The following code example demonstrates how to use the HtmlInputFile control to create a simple file upload scenario. For this example to work properly, you need to create a directory called Temp on your computer's drive C.

<%@ Page Language="C#" AutoEventWireup="True" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  void Button1_Click(object Source, EventArgs e)
  {

    if (Text1.Value == "")
    {
      Span1.InnerHtml = "Error: You must enter a file name.";
      return;
    }

    if (File1.PostedFile.ContentLength > 0)
    {
      try
      {
        File1.PostedFile.SaveAs("c:\\temp\\" + Text1.Value);
        Span1.InnerHtml = "File uploaded successfully to <b>c:\\temp\\" +
                           Text1.Value + "</b> on the Web server.";
      }
      catch (Exception exc)
      {
        Span1.InnerHtml = "Error saving file <b>c:\\temp\\" +
                           Text1.Value + "</b><br />" + exc.ToString() + ".";
      }
    }
  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head>
    <title>HtmlInputFile Example</title>
  </head>
 <body>

    <h3>HtmlInputFile Example</h3>

    <form id="form1" enctype="multipart/form-data" 
          runat="server">

       Select File to Upload: 
       <input id="File1" 
              type="file" 
              runat="server" />

       <p>
       Save as file name (no path): 
       <input id="Text1" 
              type="text" 
              runat="server" />

       </p>
       <p>
       <span id="Span1" 
             style="font: 8pt verdana;" 
             runat="server" />

       </p>
       <p>
       <input type="button" 
              id="Button1" 
              value="Upload" 
              onserverclick="Button1_Click" 
              runat="server" />

       </p>

    </form>

 </body>
 </html>

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft