This documentation is archived and is not being maintained.

HtmlInputFile Constructor

Initializes a new instance of the HtmlInputFile class.

[Visual Basic]
Public Sub New()
[C#]
public HtmlInputFile();
[C++]
public: HtmlInputFile();
[JScript]
public function HtmlInputFile();

Remarks

Use this constructor to create and initialize a new instance of the HtmlInputFile class.

The following table shows initial property values for an instance of HtmlInputFile.

Property Initial Value
TagName The "file" literal string.

Example

[Visual Basic, C#] The following example demonstrates how to create a new instance of an HtmlInputFile class.

[Visual Basic] 

<%@ Page Language="VB" AutoEventWireup="True" %>

<html>
<head>
 
   <script runat="server">
 
      Sub Button1_Click(sender As Object, e As EventArgs) 

         ' Get HtmlInputFile control from the Controls collection
         ' of the PlaceHolder control.
         Dim file As HtmlInputFile = _
            CType(Place.FindControl("File1"), HtmlInputFile)
 
         ' Make sure a file was submitted.
         If Text1.Value = "" Then 
         
            Span1.InnerHtml = "Error: you must enter a file name"
            Return
          
         End If
 
         ' Save file to server.
         If Not (file.PostedFile Is Nothing) Then

            Try
                file.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

      Sub Page_Load(sender As Object, e As EventArgs)

         ' Create a new HtmlInputFile control.
         Dim file As HtmlInputFile = New HtmlInputFile()
         file.ID = "File1"

         ' Add the control to the Controls collection of the
         ' PlaceHolder control.
         Place.Controls.Clear()
         Place.Controls.Add(file)

      End Sub 
 
   </script>
 
</head>
<body>
 
   <h3>HtmlInputFile Constructor Example</h3>
 
   <form enctype="multipart/form-data" runat="server">
 
      Specify the file to upload:
      <asp:PlaceHolder id="Place" runat="server"/> 
 
      <p>
      Save as file name (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 runat="server">
 
      void Button1_Click(object sender, EventArgs e) 
      {

         // Get the HtmlInputFile control from the Controls collection 
         // of the PlaceHolder control.
         HtmlInputFile file = (HtmlInputFile)Place.FindControl("File1");
 
         // Make sure a file was submitted.
         if (Text1.Value == "") 
         {
            Span1.InnerHtml = "Error: you must enter a file name";
            return;
         }
 
         // Save file to server.
         if (file.PostedFile != null) 
         {
            try
            {
               file.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();
            }
         }
      }

      void Page_Load(object sender, EventArgs e)
      {

         // Create a new HtmlInputFile control.
         HtmlInputFile file = new HtmlInputFile();
         file.ID = "File1";

         // Add the control to the Controls collection of the
         // PlaceHolder control.
         Place.Controls.Clear();
         Place.Controls.Add(file);

      } 
 
   </script>
 
</head>
<body>
 
   <h3>HtmlInputFile Constructor Example</h3>
 
   <form enctype="multipart/form-data" runat="server">
 
      Specify the file to upload:
      <asp:PlaceHolder id="Place" runat="server"/> 
 
      <p>
      Save as file name (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++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

HtmlInputFile Class | HtmlInputFile Members | System.Web.UI.HtmlControls Namespace

Show: