We need you!

We're working hard on the next version of Developer Fusion. Let us know what you think we should be up to!

Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Rated
Read 128,508 times

Contents

Related Categories

Uploading Files with ASP - cUpload: A closer look

cUpload: A closer look

Lets take a closer look at this cUpload class first. After a few public variables for the class, you'll notice the

Public Sub OnStartPage(PassedScriptingContext As ScriptingContext)

line, which doesn't appear to be called by anything. In fact, this procedure is called by the Active Server Pages engine when the object is initialized in our ASP script, and passes us a ScriptingContext object. This object contains all the objects available to your ASP script; Response, Request, Server etc. In this instance, we are only interested in the Request object (what has been sent to the ASP script), so we save it to our MyRequest object using

Set MyRequest = PassedScriptingContext.Request

We then proceed to call the BuildForm procedure, which parses the data sent to us via the form on upload.asp. This is where the bulk of our code lies.

As the form was posted using the encoding method, we cannot access the posted objects using the standard Request.Form object. Instead, we read the entire HTTP header into a variable. First, we get the length of the header:

varByteCount = MyRequest.TotalBytes

and then we read the header into varHTTPHeader using

varHTTPHeader = StrConv(MyRequest.BinaryRead(varByteCount), vbUnicode)

You will notice that we have also converted the header into Unicode. This is to make it easier for us to parse the posted data; instead of receiving binary data, we receive at least some readable characters. Below is a sample of what might now be present in varHTTPHeader.

-----------------------------7d130d3810678
Content-Disposition: form-data; name="thefile"; filename="C:\devpad_description.txt"
Content-Type: text/plain

Developers Pad - the ultimate programming editor. Fully configurable, with syntax highlighting, auto-indent, code library, powerful project view, quick-tag, find & replace, find in files, add-ins and more!
-----------------------------7d130d3810678
Content-Disposition: form-data; name="filedescription"


-----------------------------7d130d3810678
Content-Disposition: form-data; name="folder"

0
-----------------------------7d130d3810678--

As you can see, each field is seperated by -----------------------------7d130d3810678. Next, we are told the we are type of content, and the field name. In the case of the file, we are also given the filename and content type. Finally, we are given the value of that field. The BuildForm procedure now parses this data, and creates the correct number of form objects. As the delimiter may vary, we check it by taking the first 76 bytes:

varDelimeter = LeftB(varHTTPHeader, 76)

Next, we find where the first field is by searching for ; name="

lngFormFieldNameStart = InStrB(lngFormFieldNameStart + 1, varHTTPHeader, "; name=" & Chr(34))

Note we use the InStrB function so that we are returned the byte position rather than character position. Now, we start a Do...Loop which continues until lngFormFieldNameStart = 0. For each field, we first initialize the FormField object:

Set clsFormField = New cFormItem

Next, we find out where the name property is going to end. We do this by searching for a " character (Chr(34)), from the fields starting position, plus the length of ; name=". Now that we have the start and end positions, we can retreive the name of the field:

lngFormFieldNameEnd = InStrB(lngFormFieldNameStart + Len(StrConv("; name=" _
     & Chr(34), vbUnicode)), varHTTPHeader, Chr(34)) + Len(StrConv(Chr(34), vbUnicode))

strFormFieldName = MidB(varHTTPHeader, lngFormFieldNameStart + _
     Len(StrConv("; name=" & Chr(34), vbUnicode)), (lngFormFieldNameEnd - _
     Len(StrConv(Chr(34), vbUnicode))) - (lngFormFieldNameStart + _
     Len(StrConv("; name=" & Chr(34), vbUnicode))))

Now we check to see if there is a ; after the name field... If there is, we know it is a file, otherwise, it isn't.

If it is a file, we proceed to get the filename: and ContentType properties. After the ContentType property, we search for two vbCrLf (new lines), after which is the file data. We now save all this extra file data (filename, file size, content type etc) to the clsFormField object.

'add form field data
clsFormField.Add "FileName", strFileName
clsFormField.Add "FileLen", lngFileLength
clsFormField.Add "ContentType", strContentType

'save the files data to the collection...
clsFormField.FileData = MidB(varHTTPHeader, lngFileDataStart, lngFileLength)

If the form field isn't a file, we perform a similar operation, except we skip searching for the filename, and just get the form fields value instead. Finally, we save the fields name, and add the clsFormField object to our varFields collection:

clsFormField.Add "Name", strFormFieldName
'Assign formfieldnames and formfieldvalues to collection
varFields.Add clsFormField, strFormFieldName

We then reset the clsFormField, and search for another field to process.... and this continues until we have parsed all the fields. We then return the varFields collection for use by our ASP script.

James first started writing tutorials on Visual Basic in 1999 whilst starting this website (then known as VB Web). Since then, the site has grown rapidly, and James has written numerous tutorials, articles and reviews on VB, PHP, ASP and C#. In October 2003, James formed the company Developer Fusion Ltd, which owns this website, and also offers various development services. In his spare time, he's a 3rd year undergraduate studying Computer Science in the UK. He's also a Visual Basic MVP.

Comments

  • Invalid class string error

    Posted by selvass on 19 Apr 2005

    When i call to component in asp, when i run the uploaddemo.asp, i choose one file, once i press uploading
    showing at Invalid class string error. please help me this how to solve this.

  • A progress Bar for the Upload Component

    Posted by haguila on 12 Apr 2005

    Hi:
    The component is really good, the code works fine and it has not any problem when is impersonated via MTS.
    So, congratulations.
    From my ignorance the problem is the Event Blindness: I cannot mo...

  • Doubt

    Posted by livinvarghese on 03 Dec 2004


    Hello,

    how i can upload an Excel file which having the file Size 6MB by ASP.NET to the database.

  • ASP and VB DLL Debuggubg

    Posted by sunnysuku on 01 Apr 2004

    Please Help me !!
    How can I debug ASP pages and VB compenets . Ie I have ASP pages in Visual Interdev. These pages talking with few DLLS. I have the VB Source code for these dlls also. Please tell me...

  • what are the components of ASP?

    Posted by preetha on 01 Mar 2004

    What are the components of ASP and VB? Pls help me.