ASPNL logo (1 kb)
zaterdag 17 mei 2008




Microsoft MVP

.NET Codewise Community
<< vorige | overzicht | volgende >>

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

'Maak object, let op het New command ipv Server.CreateObject
Set oUpload = New FileUpload

'Lees het type bestand uit en stuur het naar de browser
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

'Maak object, let op het New command ipv Server.CreateObject
Set oUpload = New FileUpload

'Vraag pad en bestandsnaam op
lngPathEnd = Len(Server.MapPath(Request.ServerVariables("PATH_INFO"))) - 14
strPath = Left(Server.Mappath(Request.ServerVariables("PATH_INFO")), lngPathEnd)
strFile = "uploaded" & oUpload.FileName("bestand")

'Maak bestand en schrijf de data erin
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

'Sluit het bestand en geef de objecten vrij
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

      'Initialiseer algemene dictionary
      Set pvObjUploadRequest = Server.CreateObject("Scripting.Dictionary")
      
      'Binaire data opvragen
      RequestBin = Request.BinaryRead(Request.TotalBytes)
      
      'Ontleed binaire data
      lngPosBegin = 1
      lngPosEnd = InStrB(lngPosBegin, RequestBin, GetByteString(Chr(13)))
      Boundary = MidB(RequestBin, lngPosBegin, lngPosEnd - lngPosBegin)
      lngBoundaryPos = InStrB(1, RequestBin, Boundary)

      'Vraag alle data op
      Do Until (lngBoundaryPos = InStrB(RequestBin, Boundary & getByteString("--")))
         'Variabelen van objecten worden opgeslagen in Dictionary
         Set objUploadControl = Server.CreateObject("Scripting.Dictionary")

         'Vraag object naam op
         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)

         'Kijk of het object van type File is
         If lngPosFile <> 0 And lngPosFile < lngPosBound Then
            'Vraag de bestandsnaam, Content-type en de inhoud op
            lngPosBegin = lngPosFile + 10
            lngPosEnd = InStrB(lngPosBegin, RequestBin, GetByteString(Chr(34)))
            strFileName = GetString(MidB(RequestBin, lngPosBegin, lngPosEnd - lngPosBegin))

            'Voeg bestandsnaam toe aan Dictionary
            objUploadControl.Add "FileName", strFileName
            lngPos = InStrB(lngPosEnd, RequestBin, GetByteString("Content-Type:"))
            lngPosBegin = lngPos + 14
            lngPosEnd = InStrB(lngPosBegin, RequestBin, GetByteString(Chr(13)))

            'Voeg Content-type toe aan Dictionary
            strContentType = GetString(MidB(RequestBin, lngPosBegin, lngPosEnd - lngPosBegin))
            objUploadControl.Add "ContentType", strContentType

            'Inhoud opvragen van bestand
            lngPosBegin = lngPosEnd + 4
            lngPosEnd = InStrB(lngPosBegin, RequestBin, Boundary) - 2
            Value = MidB(RequestBin, lngPosBegin, lngPosEnd - lngPosBegin)
         Else
            'Inhoud opvragen van object
            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

         'Inhoud toevoegen aan Dictionary
         objUploadControl.Add "Value" , Value   

         'Dictionary toevoegen aan algemene Dictionary
         pvObjUploadRequest.Add strName, objUploadControl

         'Loop naar volgende object
         lngBoundaryPos = InStrB(lngBoundaryPos + LenB(Boundary), RequestBin, Boundary)
      Loop
   End Sub


   Private Sub Class_Terminate
      Dim objDictionary

      'Alle objecten leeghalen en vrijgeven
      For Each objDictionary In pvObjUploadRequest.Items
         objDictionary.RemoveAll
         Set objDictionary = Nothing
      Next
      pvObjUploadRequest.RemoveAll
      Set pvObjUploadRequest = Nothing
   End Sub


   Private Function GetByteString(strString)
   'Conversie String naar Byte String
      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)
   'Conversie Byte String naar String
      Dim intCount

      GetString = ""

      For intCount = 1 To LenB(strBin)
         GetString = GetString & Chr(AscB(MidB(strBin, intCount, 1)))
      Next
   End Function


   Public Function Value(Name)
   'Waarde opvragen van formulier veld
      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)
   'ContentType opvragen van (bestand) formulier veld
      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)
   'Bestandsnaam+Pad opvragen van (bestand) formulier veld
      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)
   'Bestandsnaam opvragen van (bestand) formulier veld
      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
%>

<< vorige | ^ naar boven | overzicht | volgende >>
copyright 2000-2007 ASPNL