• Assistenza & contatti
    • Offerte di primavera
      %

    Per pacchetti Hosting Windows (hosting ASP.NET)

    Con ASP puoi caricare un file tramite un browser. Gli script seguenti ti mostrano come fare:

    ASP

    MaxLength = x specifica la dimensione massima del file in byte.

    <head>
    <title>Dateiupload per ASP</title>
    </head>
    <body bgcolor="white">
    <%=Font%>
    <%
    ' --- ASP FileUpload Modul GetFILE (C) 1999 by Stefan Falz
    
    ' --- Attivazione della gestione errori
    ' --- On Error Resume Next
    ' --- Assegnazione della sorgente dell'errore da restituire quando si verifica un errore
    Err.Source = "GetFILE HTTP-Upload"
    ' --- Costante per l'intestazione dell'errore
    Const ErrHeader = "<b>Fehler</b><br><br>"
    
    ' --- Dichiarazione dell'array
    Dim ErrArray(4)
    ErrArray(0) = "10900 - Il file inviato è troppo grande."
    ErrArray(1) = "10901 - Errore sconosciuto.<br>"
    ErrArray(2) = "10902 - Non è stato inviato alcun file o c'è un problema nella trasmissione.<br>"
    ErrArray(3) = "10903 - Non è stato inviato alcun file di testo.<br>"
    
    ' -- Apertura della subroutine GetFILE
    Call GetFILE()
    
    
    Private Sub GetFile()
    
    ' --- In questa subroutine viene letto il flusso HTTP
    
    
    ' --- Dichiarazione delle variabili
    Dim FileText
    FileText = Request.BinaryRead(Request.TotalBytes)
    Dim FileTextByte
    FileTextByte = ""
    Dim FileTextNew
    FileTextNew = ""
    Dim FilePosFirst
    FilePosFirst = 0
    Dim FilePosLast
    FilePosLast = 0
    Dim FileType
    FileType = ""
    Dim strRevText
    strRevText = ""
    Dim strFileName
    strFileName = ""
    Dim strFileNameOnly
    strFileName = ""
    Dim posFileName
    posFileName = 0
    Dim rghFile
    rghFileName = ""
    Dim lenFileTextByte
    lenFileTextByte = 0
    
    ' --- Indicazione delle dimensioni max. del file + circa 500 byte per informazioni sul file
    Dim maxLength
    maxLength = 25500
    
    ' --- Interrogazione delle dimensioni del flusso trasmesso
    If Request.TotalBytes > maxLength Then
    Call Error_Handler(10900)
    Exit Sub
    End if
    
    ' --- Ricerca di HexCode 0D 0A 0D 0A (inizio file all'interno dello stream HTTP)
    FilePosFirst = InStrB(FileText, (ChrB(13) & ChrB(10) & ChrB(13) & ChrB(10)))
    
    ' --- Ricerca di HexCode 0D 0A 2D 2D (estensione del file all'interno delo stream HTTP)
    FilePosLast = InStrB(FileText, (ChrB(13) & ChrB(10) & ChrB(45) & ChrB(45)))
    
    ' --- RIcerca di HexCode 2D 0A 0D all'interno del flusso HTTP invertito, in quanto
    ' --- in alcuni casi sono presenti più caratteri dell'estensione di file.
    strRevText = StrReverse(FileText)
    FilePosLast = InStrB(strRevText, (ChrB(45) & ChrB(10)) & ChrB(13))
    FilePosLast = LenB(FileText) - FilePosLast
    
    ' --- Interruzione con dimensioni del file = 0 Byte
    If FilePosFirst = 0 Or FilePosLast = 0 Or FilePosLast - FilePosFirst < 5 Then
    Call Error_Handler(10902)
    Exit Sub
    End If
    ' --- Interruzione se non è specificato il paramentro "Content-Disposition"
    If InStrB(FileText, ChrB(67) & ChrB(111) & ChrB(110) & ChrB(116) & ChrB(101) & ChrB(110) & ChrB(116) & ChrB(45) & ChrB(84) & ChrB(121) & ChrB(112) & ChrB(101) & ChrB(58) & ChrB(32) & ChrB(116) & ChrB(101) & ChrB(120) & ChrB(116) & ChrB(47)) = 0 Then
    Call Error_Handler(10903)
    Exit Sub
    End if
    ' --- Interruzione nel caso in cui si sia veriificato un errore che non fa parte degli errori sopra specificati
    If Err.Number <> 0 Then
    Call Error_Handler(10901)
    Exit Sub
    End if
    ' --- Determinazione del nome del file, incluso il percorso all'interno dello stream HTTP
    posFileName = InStrB(FileText, ChrB(102) & ChrB(105) & ChrB(108) & ChrB(101) & ChrB(110) & ChrB(97) & ChrB(109) & ChrB(101) & ChrB(61) & ChrB(34)) + 9
    
    rghFileText = RightB(FileText, LenB(FileText) - posFileName)
    
    strFileName = LeftB(rghFileText, InStrB(rghFileText, ChrB(34)) - 1)
    Response.Write "<strong>Nome del file incluso il percorso: </strong>"
    Response.BinaryWrite strFileName
    Response.Write "<br>"
    
    ' --- Determinazione del nome del file, escluso il percorso all'interno dello stream HTTP
    posLastSlash = InStrB(StrReverse(strFileName), ChrB(92))
    
    strFileNameOnly = MidB(strFileName, LenB(strFileName) - posLastSlash, posLastSlash + 1)
    
    Response.Write "<strong>Nome del file escluso il percorso: </strong>"
    Response.BinaryWrite strFileNameOnly
    Response.Write "<br>"
    ' --- Interruzione se si è verificato un errore
    If Err <> 0 Then
    Call Error_Handler(10901)
    Exit Sub
    End if
    
    ' --- Scrittura del contenuto del file in ByteArray
    FileTextByte = MidB(FileText, (FilePosFirst + 4), (FilePosLast - (FilePosFirst + 4)))
    
    ' --- Determinazione delle dimensioni del file attraverso la lettura della lunghezza del contenuto del file rilevato
    lenFileTextByte = LenB(FileTextByte)
    
    ' --- Conversione del contenuto del file (stream binario) in caratteri Ascii
    For i = 1 To lenFileTextByte
    FileTextNew = FileTextNew & Chr(AscB(MidB(FileTextByte, i, 1)))
    Next
    
    ' --- Schreiben des ermittelten Dateiinhalts in eine Datei.
    Set objFileSys = Server.CreateObject("Scripting.FileSystemObject")
    Set File = objFileSys.CreateTextFile(Server.MapPath("./") & "\" & Session.SessionID & ".tmp", True, False)
    File.WriteLine CStr(FileTextNew)
    File.Close
    Set File = Nothing
    Set objFileSys = Nothing
    
    ' --- Output delle dimensionei del file in byte
    Response.Write "<strong>DDimensioni del file </strong>" & lenFileTextByte & " Byte.<br><br>"
    
    ' --- Conversione del contenuto del dile poiché le pagine HTML non vengono visualizzate correttamente
    FileTextNew = Replace(FileTextNew, "<", "&lt;")
    FileTextNew = Replace(FileTextNew, ">", "&gt;")
    FileTextNew = Replace(FileTextNew, VbCrLf, "<br>" & VbCrLf)
    FileTextNew = Replace(FileTextNew, Chr(9), "&nbsp;&nbsp;&nbsp;&nbsp;")
    
    ' --- Output del contenuto del file
    Response.Write FileTextNew
    End Sub
    
    Private Sub Error_Handler(intErrNumber)
    ' --- Determinazione del codice di errore passato e output sullo schermo
    Select Case intErrNumber
    Case 10900: Response.Write ErrHeader & ErrArray(0)
    Case 10901: Response.Write ErrHeader & ErrArray(1)
    Case 10902: Response.Write ErrHeader & ErrArray(2)
    Case 10903: Response.Write ErrHeader & ErrArray(3)
    Case Else: Response.Write ErrHeader & Err.Description
    End Select
    Exit Sub
    End Sub
    %>
    </body>
    </html>
    mixed

    Ad esempio, qui viene utilizzato come interfaccia un semplice modulo HTML.

    <html>
    <head>
    <title>Upload del file con ASP</title>
    </head>
    
    <body>
    
    <form method="POST" action="upload.asp" enctype="multipart/form-data" target="_new">
    <input type="file" name="File" size="50"><br>
    <input type="submit" value="AVVIARE UPLOAD FILE" name="Submit">
    </form>
    </body>
    </html>
    aspnet

    ASP.NET

    Con ASP.NET è possibile creare uno script di caricamento come mostrato di seguito:

    <%@ 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 = "e:\Kunden\Homepages\11\d12345678\www\UploadTest"
    
    ' 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>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>
    mixed
    Nota bene

    Assicurati che nella stringa seguente sia specificato il percorso corretto al tuo sito web:

    Dim savePath As String = "e:\cliente\siti\11\d12345678\www\UploadTest"
    aspnet