Deklarative Syntax des FileUpload-Webserver-Steuerelements

Aktualisiert: November 2007

Erstellt ein <input type=file>-Steuerelement (normalerweise als Textfeld-Steuerelement und Schaltfläche zum Suchen angezeigt), mit dem Benutzer eine Datei auswählen können, die auf den Server hochgeladen werden soll.

<asp:FileUpload
    AccessKey="string"
    BackColor="color name|#dddddd"
    BorderColor="color name|#dddddd"
    BorderStyle="NotSet|None|Dotted|Dashed|Solid|Double|Groove|Ridge|
        Inset|Outset"
    BorderWidth="size"
    CssClass="string"
    Enabled="True|False"
    EnableTheming="True|False"
    EnableViewState="True|False"
    Font-Bold="True|False"
    Font-Italic="True|False"
    Font-Names="string"
    Font-Overline="True|False"
    Font-Size="string|Smaller|Larger|XX-Small|X-Small|Small|Medium|
        Large|X-Large|XX-Large"
    Font-Strikeout="True|False"
    Font-Underline="True|False"
    ForeColor="color name|#dddddd"
    Height="size"
    ID="string"
    OnDataBinding="DataBinding event handler"
    OnDisposed="Disposed event handler"
    OnInit="Init event handler"
    OnLoad="Load event handler"
    OnPreRender="PreRender event handler"
    OnUnload="Unload event handler"
    runat="server"
    SkinID="string"
    Style="string"
    TabIndex="integer"
    ToolTip="string"
    Visible="True|False"
    Width="size"
/>

Hinweise

Das FileUpload-Steuerelement zeigt ein Textfeld-Steuerelement und eine Schaltfläche zum Suchen an, mit denen Benutzer eine Datei auf dem Client auswählen und diese auf den Webserver hochladen können. Die hochzuladende Datei wird angegeben, indem der vollständige Pfad zur Datei (z. B. C:\MyFiles\TestFile.txt) auf dem lokalen Computer im Textfeld des Steuerelements eingegeben wird. Die Datei kann auch durch Klicken auf die Schaltfläche Durchsuchen und anschließendem Navigieren zu der Datei im Dialogfeld Datei auswählen ausgewählt werden.

Das FileUpload-Steuerelement sendet eine Datei nicht automatisch an den Server, nachdem der Benutzer die hochzuladende Datei ausgewählt hat. Es muss explizit ein Steuerelement oder ein Mechanismus bereitgestellt werden, mit dem der Benutzer das Formular übermitteln kann. Normalerweise wird die Datei gespeichert oder der Inhalt wird in einer Ereignisbehandlungsmethode für ein Ereignis behandelt, das ein Postback an den Server auslöst. Wenn Sie beispielsweise eine Schaltfläche zum Übermitteln einer Datei bereitstellen, können Sie den Code zum Speichern der Datei in der Ereignisbehandlungsmethode für das Klickereignis platzieren. Weitere Informationen zum FileUpload-Steuerelement finden Sie unter Gewusst wie: Uploaden von Dateien mit dem FileUpload-Webserversteuerelement.

Beispiel

Im folgenden Codebeispiel wird das Erstellen eines FileUpload-Steuerelements veranschaulicht, das Dateien unter einem im Code angegebenen Pfad speichert. Die SaveAs-Methode wird aufgerufen, um die Datei unter dem angegebenen Pfad auf dem Server zu speichern. Die ASP.NET-Anwendung, die das Beispiel einschließt, muss über Schreibzugriff auf das angegebene Verzeichnis auf dem Server verfügen. Sie können dem Konto, unter dem die Anwendung ausgeführt wird, in dem Verzeichnis, in dem die hochgeladenen Dateien gespeichert werden sollen, explizit Schreibzugriff gewähren. Sie können auch der ASP.NET-Anwendung eine höhere Vertrauensebene gewähren.

<%@ Page Language="VB" %>

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

<script runat="server">

  Sub UploadButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)

    ' Specify the path on the server to
    ' save the uploaded file to.
    Dim savePath As String = "c:\temp\uploads\"

    ' Before attempting to perform operations
    ' on the file, verify that the FileUpload 
    ' control contains a file.
    If (FileUpload1.HasFile) Then
      ' Get the name of the file to upload.
      Dim fileName As String = FileUpload1.FileName

      ' Append the name of the file to upload to the path.
      savePath += fileName

      ' Call the SaveAs method to save the 
      ' uploaded file to the specified path.
      ' This example does not perform all
      ' the necessary error checking.               
      ' If a file with the same name
      ' already exists in the specified path,  
      ' the uploaded file overwrites it.
      FileUpload1.SaveAs(savePath)

      ' Notify the user of the name the file
      ' was saved under.
      UploadStatusLabel.Text = "Your file was saved as " & fileName

    Else
      ' Notify the user that a file was not uploaded.
      UploadStatusLabel.Text = "You did not specify a file to upload."
    End If

  End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>FileUpload Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
       <h4>Select a file to upload:</h4>

       <asp:FileUpload id="FileUpload1"                 
           runat="server">
       </asp:FileUpload>

       <br /><br />

       <asp:Button id="UploadButton" 
           Text="Upload file"
           OnClick="UploadButton_Click"
           runat="server">
       </asp:Button>    

       <hr />

       <asp:Label id="UploadStatusLabel"
           runat="server">
       </asp:Label>        
    </div>
    </form>
</body>
</html>
<%@ Page Language="C#" %>

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

<script runat="server">

  protected void UploadButton_Click(object sender, EventArgs e)
  {
    // Specify the path on the server to
    // save the uploaded file to.
    String savePath = @"c:\temp\uploads\";

    // Before attempting to perform operations
    // on the file, verify that the FileUpload 
    // control contains a file.
    if (FileUpload1.HasFile)
    {
      // Get the name of the file to upload.
      String fileName = FileUpload1.FileName;

      // Append the name of the file to upload to the path.
      savePath += fileName;


      // Call the SaveAs method to save the 
      // uploaded file to the specified path.
      // This example does not perform all
      // the necessary error checking.               
      // If a file with the same name
      // already exists in the specified path,  
      // the uploaded file overwrites it.
      FileUpload1.SaveAs(savePath);

      // Notify the user of the name of the file
      // was saved under.
      UploadStatusLabel.Text = "Your file was saved as " + fileName;
    }
    else
    {      
      // Notify the user that a file was not uploaded.
      UploadStatusLabel.Text = "You did not specify a file to upload.";
    }

  }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>FileUpload Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
       <h4>Select a file to upload:</h4>

       <asp:FileUpload id="FileUpload1"                 
           runat="server">
       </asp:FileUpload>

       <br /><br />

       <asp:Button id="UploadButton" 
           Text="Upload file"
           OnClick="UploadButton_Click"
           runat="server">
       </asp:Button>    

       <hr />

       <asp:Label id="UploadStatusLabel"
           runat="server">
       </asp:Label>        
    </div>
    </form>
</body>
</html>

Siehe auch

Konzepte

Übersicht über das FileUpload-Webserversteuerelement

Referenz

FileUpload