Freigeben über


HtmlInputFile.MaxLength-Eigenschaft

Ruft die maximale Dateipfadlänge für den Upload einer Datei vom Clientcomputer ab oder legt diese fest.

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

Syntax

'Declaration
Public Property MaxLength As Integer
'Usage
Dim instance As HtmlInputFile
Dim value As Integer

value = instance.MaxLength

instance.MaxLength = value
public int MaxLength { get; set; }
public:
property int MaxLength {
    int get ();
    void set (int value);
}
/** @property */
public int get_MaxLength ()

/** @property */
public void set_MaxLength (int value)
public function get MaxLength () : int

public function set MaxLength (value : int)

Eigenschaftenwert

Die maximale Dateipfadlänge. Der Standardwert ist -1 und gibt an, dass die Eigenschaft nicht festgelegt wurde.

Hinweise

Verwenden Sie diese Eigenschaft, um die Zeichenanzahl für den Pfad der Uploaddatei zu beschränken.

Hinweis

Die Unterstützung für diese Eigenschaft ist vom jeweiligen Browser abhängig. Überprüfen Sie, ob Ihr Browser diese Eigenschaft unterstützt.

Beispiel

Im folgenden Codebeispiel wird veranschaulicht, wie Sie mithilfe der MaxLength-Eigenschaft die mögliche Zeichenanzahl des Dateipfades beschränken. Damit dieses Beispiel ordnungsgemäß ausgeführt werden kann, müssen Sie auf dem Laufwerk C des Computers ein Verzeichnis mit der Bezeichnung Temp erstellen.

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

<script language="VB" runat="server">

  Sub Button1_Click(ByVal Source As Object, ByVal e As EventArgs)
      
    ' Make sure a file was submitted.
    If Text1.Value = "" Then
      
      Span1.InnerHtml = "Error: You must enter a file name."
      Return
      
    End If
        
    ' Save the file.
    If File1.PostedFile.ContentLength > 0 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
  
</script>

<html>
  <head>
    <title>HtmlInputFile Example</title> 
  </head>
 
  <body>
 
    <h3>HtmlInputFile Sample</h3>
 
    <form enctype="multipart/form-data" 
          runat="server">
 
       Select File to Upload: 
       <input id="File1" 
              type="file"
              maxlength="30"
              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>
<%@ Page Language="C#" AutoEventWireup="True" %>

<script runat="server">
 
  void Button1_Click(object Source, EventArgs e)
  {

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

    // Save the file.
    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() + ".";
      }
    }
    Span1.InnerHtml = File1.MaxLength.ToString();
  }
 
</script>

<html>
  <head>
    <title>HtmlInputFile Example</title>
  </head>

  <body>
 
    <h3>HtmlInputFile Sample</h3>
 
    <form enctype="multipart/form-data" 
          runat="server">
 
       Select File to Upload: 
       <input id="File1" 
              type="file"
              maxlength="30"
              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> 
<%@ Page Language="JScript" AutoEventWireup="True" %>

<script runat="server">

  function Button1_Click(source : Object, e : EventArgs){
      
    // Make sure a file was submitted.
    if(Text1.Value == "")
    {

      Span1.InnerHtml = "Error: You must enter a file name."
      return

    }
    
    // Save the file.
    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(exc : Exception)
        {

            Span1.InnerHtml = "Error saving file <b>c:\\temp\\"
                            + Text1.Value + "</b><br>" + exc.ToString() + "."

        }
    }

  }

</script>

<html>
  <head>
    <title>HtmlInputFile Example</title>
  </head>
 
  <body>
 
    <h3>HtmlInputFile Sample</h3>
 
    <form enctype="multipart/form-data" 
          runat="server">
 
       Select File to Upload: 
       <input id="File1" 
              type="file"
              maxlength="30"
              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>

Plattformen

Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

Siehe auch

Referenz

HtmlInputFile-Klasse
HtmlInputFile-Member
System.Web.UI.HtmlControls-Namespace
Size

Weitere Ressourcen

HTML-Serversteuerelemente