HtmlInputFile Class
Allows programmatic access to the HTML <input type= file> element on the server.
For a list of all members of this type, see HtmlInputFile Members.
System.Object
System.Web.UI.Control
System.Web.UI.HtmlControls.HtmlControl
System.Web.UI.HtmlControls.HtmlInputControl
System.Web.UI.HtmlControls.HtmlInputFile
[Visual Basic] Public Class HtmlInputFile Inherits HtmlInputControl Implements IPostBackDataHandler [C#] public class HtmlInputFile : HtmlInputControl, IPostBackDataHandler [C++] public __gc class HtmlInputFile : public HtmlInputControl, IPostBackDataHandler [JScript] public class HtmlInputFile extends HtmlInputControl implements IPostBackDataHandler
Thread Safety
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Remarks
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.
Note The Enctype property of an HtmlForm must be set to "multipart/form-data" for this control to work properly.
To specify the file to upload, enter the fully 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.
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 contained in the PostedFile property. This operation is usually done in an event-handling method, such as for a button click.
Note When you use the SaveAs method, make sure to specify the full path to the file (for example, "C:\MyFiles\Test.txt"). Otherwise, an attempt is made 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 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 of the Web.config file for the application.
For a list of initial property values for an instance of HtmlInputFile, see the HtmlInputFile constructor.
Example
[Visual Basic, C#, JScript] This 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 C: drive.
[Visual Basic] <%@ Page Language="VB" AutoEventWireup="True" %> <html> <head> <script language="VB" runat="server"> Sub Button1_Click(Source As Object, e As EventArgs) If Text1.Value = "" Then Span1.InnerHtml = "Error: you must enter a file name" Return End If If Not (File1.PostedFile Is Nothing) Then 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 exc As Exception Span1.InnerHtml = "Error saving file <b>c:\temp\" & _ Text1.Value & "</b><br>" & exc.ToString() End Try End If End Sub 'Button1_Click </script> </head> <body> <h3>HtmlInputFile Sample</h3> <form enctype="multipart/form-data" runat="server"> Select File to Upload: <input id="File1" type="file" runat="server"> <p> Save as filename (no path): <input id="Text1" type="text" runat="server"> <p> <span id=Span1 style="font: 8pt verdana;" runat="server" /> <p> <input type=button id="Button1" value="Upload" OnServerClick="Button1_Click" runat="server"> </form> </body> </html> [C#] <%@ Page Language="C#" AutoEventWireup="True" %> <html> <head> <script language="C#" 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 != null) { 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> </head> <body> <h3>HtmlInputFile Sample</h3> <form enctype="multipart/form-data" runat="server"> Select File to Upload: <input id="File1" type="file" runat="server"> <p> Save as filename (no path): <input id="Text1" type="text" runat="server"> <p> <span id=Span1 style="font: 8pt verdana;" runat="server" /> <p> <input type=button id="Button1" value="Upload" OnServerClick="Button1_Click" runat="server"> </form> </body> </html> [JScript] <%@ Page Language="JScript" AutoEventWireup="True" %> <html> <head> <script language="JSCRIPT" runat="server"> function Button1_Click(source : Object, e : EventArgs){ if(Text1.Value == ""){ Span1.InnerHtml = "Error: you must enter a file name" return } if(File1.PostedFile != null){ 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(exc : Exception){ Span1.InnerHtml = "Error saving file <b>c:\\temp\\" + Text1.Value + "</b><br>" + exc.ToString() } } } </script> </head> <body> <h3>HtmlInputFile Sample</h3> <form enctype="multipart/form-data" runat="server"> Select File to Upload: <input id="File1" type="file" runat="server"> <p> Save as filename (no path): <input id="Text1" type="text" runat="server"> <p> <span id=Span1 style="font: 8pt verdana;" runat="server" /> <p> <input type=button id="Button1" value="Upload" OnServerClick="Button1_Click" runat="server"> </form> </body> </html>
[C++] No example is available for C++. To view a Visual Basic, C#, or JScript example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Namespace: System.Web.UI.HtmlControls
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family
Assembly: System.Web (in System.Web.dll)
See Also
HtmlInputFile Members | System.Web.UI.HtmlControls Namespace | HttpPostedFile | PostedFile | HtmlInputControl | Enctype | <httpRuntime> Element | <processModel> Element