Caricare un file con ASP
Per creare un PDF, utilizzare la funzione “Stampa” in fondo alla pagina.
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>
<bodybgcolor="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()
PrivateSub 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
IfRequest.TotalBytes > maxLength Then
Call Error_Handler(10900)
ExitSub
Endif
' --- 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 = 0Or FilePosLast = 0Or FilePosLast - FilePosFirst < 5Then
Call Error_Handler(10902)
ExitSub
EndIf
' --- 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)) = 0Then
Call Error_Handler(10903)
ExitSub
Endif
' --- Interruzione nel caso in cui si sia veriificato un errore che non fa parte degli errori sopra specificati
IfErr.Number <> 0Then
Call Error_Handler(10901)
ExitSub
Endif
' --- 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
IfErr <> 0Then
Call Error_Handler(10901)
ExitSub
Endif
' --- 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 = 1To 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, "<", "<")
FileTextNew = Replace(FileTextNew, ">", ">")
FileTextNew = Replace(FileTextNew, VbCrLf, "<br>" & VbCrLf)
FileTextNew = Replace(FileTextNew, Chr(9), " ")
' --- Output del contenuto del file
Response.Write FileTextNew
EndSub
PrivateSub Error_Handler(intErrNumber)
' --- Determinazione del codice di errore passato e output sullo schermo
SelectCase intErrNumber
Case10900: Response.Write ErrHeader & ErrArray(0)
Case10901: Response.Write ErrHeader & ErrArray(1)
Case10902: Response.Write ErrHeader & ErrArray(2)
Case10903: Response.Write ErrHeader & ErrArray(3)
CaseElse: Response.Write ErrHeader & Err.Description
EndSelect
ExitSub
EndSub
%>
</body>
</html>
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>
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 onthe server to
' save the uploaded fileto.
Dim savePath As String = "e:\Kunden\Homepages\11\d12345678\www\UploadTest"
' Before attempting to perform operations
' onthefile, verify thatthe FileUpload
' control contains a file.
If (FileUpload1.HasFile) Then
' Get thenameofthefileto upload.
Dim fileName As String = FileUpload1.FileName
' Append thenameofthefileto upload tothe path.
savePath += fileName
' Call the SaveAs method to save the
' uploaded filetothe specified path.
' This example doesnot perform all
' the necessary error checking.
' If a filewiththe same name
' already exists inthe specified path,
' the uploaded file overwrites it.
FileUpload1.SaveAs(savePath)
' Notify the user ofthenamethefile
' 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>
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"