Een bestand uploaden zonder component
Door Wouter Boevink
en Michiel van Otegem
20 april 2001
Als je een bestand wilt uploaden naar de server, hoeft dit in ASP niet
persé met een component. Je kan het ook alleen met script doen,
maar dat is wel een stuk ingewikkelder. Dit komt omdat je niet meer
gebruik kan maken van de normale methoden van het Request object om
de waarden uit het formulier op te vragen.
(zie ook het artikel Een bestand uploaden naar de server)
je moet daarom binaire data manipuleren en dat is een vervelende klus.
Hieronder vind je code voor het uploaden van bestanden via script.
Je hoeft dus niet een component te registreren op de server, je hoeft
alleen het script te gebruiken. Omdat deze code in een script Class staat,
kun je deze net zo aanroepen alsof het een normaal component is.
Wouter heeft de code geschreven om de binaire gegevens over te zetten
in een Dictionary object, welke vervolgens bijna als het Request object
gebruikt kan worden. Michiel heeft het vervolgens herschreven zodat het
als object gebruikt kan worden. Hieronder zie je hoe eenvoudig het is.
Helemaal onderaan staat de bron-code van clsFileUpload.asp. Dat is de
code die het allemaal doet. Het enige wat je hoeft te doen is die
in je code includen.
Voorbeelden
De code hieronder leest de waarden in het formulier (inclusief het
meegestuurde bestand) en geeft het bestand weer.
uploadscript1.asp
<%
Option Explicit
Dim oUpload
Set oUpload = New FileUpload
Response.ContentType = oUpload.ContentType("BESTAND")
Response.BinaryWrite oUpload.Value("Bestand")
Set oUpload = Nothing
%>
<!--#include file="clsFileUpload.asp"-->
Je kunt ook de waarden in het formulier opvragen en het bestand
bijvoorbeeld opslaan.
uploadscript2.asp
<%
Option Explicit
Dim oUpload
Dim oFSO, oFile
Dim lngPathEnd
Dim strPath, strFile
Dim i
Set oUpload = New FileUpload
lngPathEnd = Len(Server.MapPath(Request.ServerVariables("PATH_INFO"))) - 14
strPath = Left(Server.Mappath(Request.ServerVariables("PATH_INFO")), lngPathEnd)
strFile = "uploaded" & oUpload.FileName("bestand")
Set oFSO = Server.CreateObject("Scripting.FileSystemObject")
Set oFile = oFSO.CreateTextFile(strPath & strFile)
For i = 1 To LenB(oUpload.Value("bestand"))
oFile.Write Chr(AscB(MidB(oUpload.Value("bestand"), i, 1)))
Next
oFile.Close
Set oFile = Nothing
Set oFSO = Nothing
Response.Write "Bedankt " & oUpload.Value("Naam") & ".<br>"
Response.Write "Het bestand is opgeslagen als " & strPath & strFile
Set oUpload = Nothing
%>
<!--#include file="clsFileUpload.asp"-->
Als voorbeeld gebruiken we de onderstaande formulieren. Let erop dat de
formulieren specifiek met ENCTYPE="multipart/form-data"
werken, om aan te geven dat er bestanden meegestuurd kunnen worden.
uploadform.asp
<HTML>
<BODY>
<FORM METHOD="Post" ENCTYPE="multipart/form-data" ACTION="uploadscript1.asp">
Naam: <INPUT TYPE="Text" NAME="Naam"><BR>
Bestand: <INPUT TYPE="file" NAME="Bestand"><BR>
<INPUT TYPE="submit" Value="Verstuur naar uploadscript1.asp">
</FORM>
<HR>
<FORM METHOD="Post" ENCTYPE="multipart/form-data" ACTION="uploadscript2.asp">
Naam: <INPUT TYPE="Text" NAME="Naam"><BR>
Bestand: <INPUT TYPE="file" NAME="Bestand"><BR>
<INPUT TYPE="submit" Value="Verstuur naar uploadscript2.asp">
</FORM>
</BODY>
</HTML>
clsFileUpload.asp
<%
Class FileUpload
Private pvObjUploadRequest
Private Sub Class_Initialize
Dim RequestBin, Boundary, Value
Dim lngPosBegin, lngPosEnd, lngBoundaryPos
Dim lngPos, lngPosFile, lngPosBound
Dim strName, strFileName, strContentType
Dim objUploadControl
Set pvObjUploadRequest = Server.CreateObject("Scripting.Dictionary")
RequestBin = Request.BinaryRead(Request.TotalBytes)
lngPosBegin = 1
lngPosEnd = InStrB(lngPosBegin, RequestBin, GetByteString(Chr(13)))
Boundary = MidB(RequestBin, lngPosBegin, lngPosEnd - lngPosBegin)
lngBoundaryPos = InStrB(1, RequestBin, Boundary)
Do Until (lngBoundaryPos = InStrB(RequestBin, Boundary & getByteString("--")))
Set objUploadControl = Server.CreateObject("Scripting.Dictionary")
lngPos = InStrB(lngBoundaryPos, RequestBin, GetByteString("Content-Disposition"))
lngPos = InStrB(lngPos, RequestBin, GetByteString("name="))
lngPosBegin = lngPos + 6
lngPosEnd = InStrB(lngPosBegin, RequestBin, GetByteString(Chr(34)))
strName = LCase(GetString(MidB(RequestBin, lngPosBegin, lngPosEnd - lngPosBegin)))
lngPosFile = InStrB(lngBoundaryPos, RequestBin, GetByteString("filename="))
lngPosBound = InStrB(lngPosEnd, RequestBin, Boundary)
If lngPosFile <> 0 And lngPosFile < lngPosBound Then
lngPosBegin = lngPosFile + 10
lngPosEnd = InStrB(lngPosBegin, RequestBin, GetByteString(Chr(34)))
strFileName = GetString(MidB(RequestBin, lngPosBegin, lngPosEnd - lngPosBegin))
objUploadControl.Add "FileName", strFileName
lngPos = InStrB(lngPosEnd, RequestBin, GetByteString("Content-Type:"))
lngPosBegin = lngPos + 14
lngPosEnd = InStrB(lngPosBegin, RequestBin, GetByteString(Chr(13)))
strContentType = GetString(MidB(RequestBin, lngPosBegin, lngPosEnd - lngPosBegin))
objUploadControl.Add "ContentType", strContentType
lngPosBegin = lngPosEnd + 4
lngPosEnd = InStrB(lngPosBegin, RequestBin, Boundary) - 2
Value = MidB(RequestBin, lngPosBegin, lngPosEnd - lngPosBegin)
Else
lngPos = InStrB(lngPos, RequestBin, GetByteString(Chr(13)))
lngPosBegin = lngPos + 4
lngPosEnd = InStrB(lngPosBegin, RequestBin, Boundary) - 2
Value = GetString(MidB(RequestBin, lngPosBegin, lngPosEnd - lngPosBegin))
End If
objUploadControl.Add "Value" , Value
pvObjUploadRequest.Add strName, objUploadControl
lngBoundaryPos = InStrB(lngBoundaryPos + LenB(Boundary), RequestBin, Boundary)
Loop
End Sub
Private Sub Class_Terminate
Dim objDictionary
For Each objDictionary In pvObjUploadRequest.Items
objDictionary.RemoveAll
Set objDictionary = Nothing
Next
pvObjUploadRequest.RemoveAll
Set pvObjUploadRequest = Nothing
End Sub
Private Function GetByteString(strString)
Dim Char
Dim i
For i = 1 To Len(strString)
Char = Mid(strString, i , 1)
GetByteString = GetByteString & ChrB(AscB(Char))
Next
End Function
Private Function GetString(strBin)
Dim intCount
GetString = ""
For intCount = 1 To LenB(strBin)
GetString = GetString & Chr(AscB(MidB(strBin, intCount, 1)))
Next
End Function
Public Function Value(Name)
Name = LCase(Name)
If pvObjUploadRequest.Exists(Name) Then
Value = pvObjUploadRequest.Item(Name).Item("Value")
Else
Value = Empty
End If
End Function
Public Function ContentType(Name)
Name = LCase(Name)
If pvObjUploadRequest.Exists(Name) Then
If pvObjUploadRequest.Item(Name).Exists("ContentType") Then
ContentType = pvObjUploadRequest.Item(Name).Item("ContentType")
Else
ContentType = Empty
End If
Else
ContentType = Empty
End If
End Function
Public Function FileNamePath(Name)
Name = LCase(Name)
If pvObjUploadRequest.Exists(Name) Then
If pvObjUploadRequest.Item(Name).Exists("FileName") Then
FileNamePath = pvObjUploadRequest.Item(Name).Item("FileName")
Else
FileNamePath = Empty
End If
Else
FileNamePath = Empty
End If
End Function
Public Function FileName(Name)
Dim strFileName
Name = LCase(Name)
If pvObjUploadRequest.Exists(Name) Then
If pvObjUploadRequest.Item(Name).Exists("FileName") Then
strFileName = pvObjUploadRequest.Item(Name).Item("FileName")
FileName = Right(strFileName, Len(strFileName) - InstrRev(strFileName, "\"))
Else
FileName = Empty
End If
Else
FileName = Empty
End If
End Function
End Class
%>
|