| Beispiel
Zum schnellen Einstieg möchten wir Ihnen im folgenden zwei kurze Beispielskripte (eins für VBScript, eins für JScript) zeigen, das alle Formularfelder sowie Informationen zu allen übertragenen Files auflistet und die Dateien im Unterverzeichnis 'Upload' unter Ihrem ursprünglichen Dateinamen speichert.
Wir gehen davon aus, dass sich die Datei 'FileUploadJS.asp' im gleichen Verzeichnis wie das Skript befindet, sowie zwei Unterverzeichnisse 'temp' und 'upload' existieren, für die der (anonyme) Benutzer Schreibrechte besitzt.
<%@ Language=JScript %>
<!-- #include file="FileUploadJS.asp" -->
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
</HEAD>
<BODY>
<%
try {
Upload = new cFileUpload()
Upload.Error.Mode = emJScript;
Upload.Error.Level = elExtended;
Upload.PathToTemp = Server.MapPath('./') + '\\temp\\';
Upload.Upload();
Response.Write('Form-Dictionary:<table border="1"><tr><td>Name</td><td>Wert</td></tr>');
for (e = new Enumerator(Upload.Form); !e.atEnd(); e.moveNext())
Response.Write('<tr><td>' + e.item() + '</td><td>' + Upload.Form(e.item()) + '</td></tr>');
Response.Write('</table>');
Response.Write('<br>Files-Dictionary:<table border="1"><tr><td>Name</td><td>Pfad</td>'
+ '<td>Dateiname</td><td>Erweiterung</td><td>Content-Type</td><td>Gespeichert unter</td></tr>');
for (e = new Enumerator(Upload.Files); !e.atEnd(); e.moveNext()){
Response.Write('<tr><td>' + e.item() + '</td><td>' + Upload.Files(e.item()).FilePath + '</td><td>'
+ Upload.Files(e.item()).FileName + '</td><td>' + Upload.Files(e.item()).FileExt + '</td><td>'
+ Upload.Files(e.item()).ContentType) + '</td>';
if (!Upload.Files(e.item()).IsEmpty) {
Upload.Files(e.item()).SaveAs(Server.MapPath('./') + '\\upload\\', Upload.Files(e.item()).FileName
+ '.' + Upload.Files(e.item()).FileExt, true);
Response.Write('<td>' + Upload.Files(e.item()).SavedAs + '</td>');
}
Response.Write('</tr>');
}
Response.Write('</table>');
} catch(e) {
Response.Write('<br>' + e.description);
}
%>
</BODY>
</HTML>
Das gleiche Beispiel sieht in VBScript folgendermaßen aus:
<%@ Language=VBScript %>
<!-- #include file="FileUploadJS.asp" -->
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
</HEAD>
<BODY>
<%
On Error Resume Next
set Upload = FileUpload()
Upload.Error.Level = elExtended
Upload.Error.Mode = emVBScript
Upload.PathToTemp = Server.MapPath("./") & "\temp\"
Upload.Upload()
Response.Write "Form-Dictionary:<table border=1><tr><td>Name</td><td>Wert</td></tr>"
for each item in Upload.Form
Response.Write "<tr><td>" & Item & "</td><td>" & Upload.Form(Item) & "</td></tr>"
next
Response.Write "</table>"
Response.Write "<br>Files-Dictionary:<table border=1><tr><td>Name</td><td>Pfad</td>
<td>Dateiname</td><td>Erweiterung</td><td>Content-Type</td><td>Gespeichert unter</td></tr>"
for each Item in Upload.Files
Response.Write "<tr><td>" & Item & "</td><td>" & Upload.Files(Item).FilePath & "</td><td>"
& Upload.Files(Item).FileName & "</td><td>" & Upload.Files(Item).FileExt & "</td><td>"
& Upload.Files(Item).ContentType & "</td>"
if not(Upload.Files(Item).IsEmpty) then
Upload.Files(Item).SaveAs Server.MapPath("./") & "\upload\", Upload.Files(Item).FileName
& "." & Upload.Files(Item).FileExt, true
Response.Write "<td>" & Upload.Files(Item).SavedAs & "</td>"
end if
Response.Write "</tr>"
next
Response.Write "</table>"
Response.Write "<br>Error # " & CStr(Err.Number) & " " & Err.Description
Err.Clear
%>
</BODY>
</HTML>
Eine detaillierte Zusammenstellung aller zur Verfügung stehender Methoden und Eigenschaften finden Sie in der Referenz im letzten Teil dieses Beitrages. |