Condividi tramite


Sintassi dichiarativa per il controllo server HtmlInputFile

Crea un controllo lato server che viene mappato all'elemento HTML <input type=file> e consente di caricare un file nel server.

<input
    Type="File"
    EnableViewState="False|True"
    Id="string"
    Visible="False|True"
    OnDataBinding="OnDataBinding event handler"
    OnDisposed="OnDisposed event handler"
    OnInit="OnInit event handler"
    OnLoad="OnLoad event handler"
    OnPreRender="OnPreRender event handler"
    OnUnload="OnUnload event handler"
    runat="server"
    />

Note

Utilizzare il controllo HtmlInputFile per eseguire la programmazione in base all'elemento HTML <input type=file>. È possibile utilizzare il controllo HtmlInputFile per progettare una pagina che consenta agli utenti di caricare file binari o file di testo da un browser in una directory designata sul server Web. Il caricamento dei file è consentito su tutti i browser HTML 3.2 e successivi.

Esempio

Nell'esempio seguente viene illustrata una situazione di caricamento semplice. La prima sezione di codice definisce il gestore eventi per la pagina. Quando si fa clic sul pulsante Upload nel form, il nome file, la lunghezza e la quantità del contenuto (in byte) sono visualizzati sulla pagina, mentre il file viene caricato nella directory UploadedFiles sul server.

NotaNota

È necessario impostare l'attributo enctype del form su "multipart/form-data".

Il codice del form implementa un controllo HtmlForm, un controllo HtmlInputFile, un controllo HtmlInputButton e quattro HtmlGenericControls (l'elemento <div> e tre elementi <span>, ognuno dei quali con le coppie di attributo/valore runat="server" nei relativi tag di apertura).

NotaNota

Per visualizzare le informazioni sul file caricato nella pagina, è necessario impostare la proprietà Visible, che HtmlGenericControl eredita dalla classe Control, su true nel codice del gestore eventi.

<%@ Page Language="VB" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>HtmlInputFile Control</title>
</head>

   <script runat="server">
      Sub UploadBtn_Click(Sender as Object, e as EventArgs)

         ' Display information about posted file
         FileName.InnerHtml = MyFile.PostedFile.FileName
         MyContentType.InnerHtml = MyFile.PostedFile.ContentType 
         ContentLength.InnerHtml = cStr(MyFile.PostedFile.ContentLength)
         FileDetails.Visible = True

         ' Save uploaded file to server
         MyFile.PostedFile.SaveAs("c:\Uploadedfiles\uploadfile.txt")
      End Sub
   </script>

   <body>
      <form id="Form1" action="fileupload.aspx" 
            method="post"
            enctype="multipart/form-data" 
            runat="server">

         <h1>ASP.NET File Upload Example</h1>
         Select File To Upload to Server: 
         <input id="MyFile" 
                type="file" 
                runat="server" /> 
         <br /><br />
         <input id="Submit1" type="submit" 
                value="Upload!"
                onserverclick="UploadBtn_Click" 
                runat="server" />
         <br /><br /><br />
         <div id="FileDetails" 
              visible="false" 
              runat="server">
            FileName: <span id="FileName" runat="server"/> <br />
            ContentType: <span id="MyContentType" runat="server"/> <br />
            ContentLength: <span id="ContentLength" runat="server"/>bytes
            <br />
         </div>
      </form>
   </body>
</html>
<%@ 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">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>HtmlInputFile Control</title>
</head>

   <script runat="server">
      void UploadBtn_Click(Object sender, EventArgs e)
      {
         // Display information about posted file
         FileName.InnerHtml = MyFile.PostedFile.FileName;
         MyContentType.InnerHtml = MyFile.PostedFile.ContentType; 
         ContentLength.InnerHtml =
                               MyFile.PostedFile.ContentLength.ToString();
         FileDetails.Visible = true;

         // Save uploaded file to server
         MyFile.PostedFile.SaveAs("c:\\Uploadedfiles\\uploadfile.txt");
      }
   </script>

   <body>
      <form id="Form1" action="fileupload.aspx" 
            method="post"
            enctype="multipart/form-data" 
            runat="server">

         <h1>ASP.NET File Upload Example</h1>
         Select File To Upload to Server: 
         <input id="MyFile" 
                type="file" 
                runat="server" /> 
         <br /><br />
         <input id="Submit1" type="submit" 
                value="Upload!"
                onserverclick="UploadBtn_Click" 
                runat="server" />
         <br /><br /><br />
         <div id="FileDetails" 
              visible="false" 
              runat="server">
            FileName: <span id="FileName" runat="server"/> <br />
            ContentType: <span id="MyContentType" runat="server"/> <br />
            ContentLength: <span id="ContentLength" runat="server"/>bytes
            <br />
         </div>
      </form>
   </body>
</html>

Vedere anche

Riferimenti

HtmlInputFile

Altre risorse

Controlli server HTML