Click here to Skip to main content
15,881,588 members
Articles / Desktop Programming / ATL
Article

Advanced ASP Uploader

Rate me:
Please Sign up or sign in to vote.
4.95/5 (17 votes)
28 Aug 20013 min read 440.6K   7.5K   61   80
An advanced COM component that provides file upload capabilities for your ASP pages.

Introduction

Browser-based (HTTP) file uploading is a great way to transfer arbitrary files from a client machine to the Web server which adds another dimension to Web-based applications. However, to enable this feature the client form should be submitted using "multipart/form-data" encoding.

Unfortunately, the ASP Request.Form object cannot be used with this encoding type (currently). For this reason, many solutions have been provided by third party components to solve this problem. However, most of these third party components suffer from one or more of the following problems:

  1. Discarding other important form data which are not related to the uploaded file.
  2. No support for multiple file uploading.
  3. Inconsistent interface to the existing Request.Form interface.
  4. No support for chunk data reading from the uploaded file (helpful on uploading a large. file into a BLOB field in a database using AppendChunk in an ADO Recordset object).

For these reasons, I have developed a COM object that users can use instead of Request.Form in case of a "multipart/form-data" encoding.

While developing this COM object, I have tried to avoid falling into any of the previous mentioned problems. However, the solution that I am providing will not totally replace Request.Form, rather it will be a complementary solution that users can use incase of a "multipart/form-data" encoding as I mentioned earlier. Users can still use Request.Form for other encoding types, however.

What is multipart/form-data?

This "multipart/form-data" type of encoding is described in RFC1867 which describes how file uploading should be implemented over HTTP protocol. To enable file uploading the form enctype should be set to multipart/form-data and the method should be set to post. Then the input tag should be of type file.

For the previous form here is the required code

<form method=post action=upload.asp enctype="multipart/form-data">
<input name=name value="Al-Mutairi, Fayez">
<input name=email value="fayezmm@yahoo.com">
<input name=file type=file>
<input name=submit type=submit value=Submit>
</form>

When the user clicks the submit button the web server will receive the following:

-----------------------------7d1f5a80420
Content-Disposition: form-data; name="name"

Al-Mutairi, Fayez
-----------------------------7d1f5a80420
Content-Disposition: form-data; name="email"

fayezmm@yahoo.com
-----------------------------7d1f5a80420
Content-Disposition: form-data; name="file"; filename="C:\folder_icon.gif"
Content-Type: image/gif

GIF89a2 2 ³ÿ ÿÿÿÌÌÿ™™ÿffÌUUUDDD33f"""                        ,    2 2 @
éÈI«½8ëÍ»¯H Ž" … ,‹|]HÎ49 7ì!Å Ô³[NGÔ Ž‡CqÉl:‹¯Sê'@žfB©<¯§v
†±ž
φC$
„+zN¯Ûün$u§/z-Vvc@*<,{bdd UX†ŽlCb=?•#f|jš4fqŒžŸ=>fo¦­®¯°±²³N
G¬´Ÿ<_z³2ˆT„Á…\R_-ƒ¥µÆ+ƒs"‡ÍŠÃMÑŽ]*gÖ›5ÝAm"áâ—Ökå/ßh Ýìuî[£­ò–npÊ…™"
ª(úø¨ió/ ®ƒ*¬/ ;
<!--"-->
-----------------------------7d1f5a80420
Content-Disposition: form-data; name="submit"

Submit
-----------------------------7d1f5a80420--

At this time when upload.asp started it cannot access the form data using Request.Form So it should use a third party component for help. The component will read the previous request, parse it and build a collection of items that can be accessed in a similar fashion to accessing Request.Form items.

How To Use the Component

Let us build upload.asp using our MyRequest.Form component

<%@ LANGUAGE="JScript%">
<%
    var form = Server.CreateObject("MyRequest.Form");
    form.Init();
    Response.Write("Name =" + form("name"));
    Response.Write("Email =" + form("email"));
    var file = form.Item("file").Item(1);
    Response.Write("FileName =" + file.FileName);
    Response.Write("FileExt  =" + file.FileExt);
    Response.Write("FilePath =" + file.FilePath);
    Response.Write("MimeType =" + file.ContentType);
    Response.Write("FileSize =" + file.TotalBytes);

    // ** To save into a file use Write method of the ADO Stream Object
    var fileStream = Server.CreateObject("ADODB.Stream");
    fileStream.Open();
    fileStream.Type = adTypeBinary;
    fileStream.Write(file.Value);
    fileStream.SaveToFile(Server.MapPath("Uploaded Files") + "\\" + file.FileName, adSaveCreateOverWrite);
    fileStream.Close();

    // ** To save into a database use AppendChunk method of the Field Object of ADO Recordset
    var cn = Server.CreateObject("ADODB.Connection");
    var rs = Server.CreateObject("ADODB.Recordset");
    cn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("Database.mdb"));
    rs.Open("Users", cn, adOpenForwardOnly, adLockOptimistic, adCmdTable);
    rs.AddNew();

    rs.Fields("id") = 1000;
    rs.Fields("name") = form("name").Value;
    rs.Fields("email") = form("email").Value;
    rs.Fields("FileType") = file.ContentType;
    rs.Fields("file").AppendChunk(file.Value);

    rs.Update();
    rs.Close();
%>

Interfaces


Here is the IForm Interface

MethodDescription
Init(VARIANT varBinary) Initialize the form with posted data. varBinary is optional it is =Request.BinaryRead(Request.TotalBytes)
Item(VARIANT Index, IListItem** ppItem) (Default method) Returns the ListItem object with the specified key either as the item name or index.
Count(long* pVal) Number of listitem's objects.

Here is the IListItem Interface

MethodDescription
Value(VARIANT* pValue) (Default method) Item's value.
Item(long lIndex, IItem** ppItem) Returns the Item object with the specified index.
Count(long* pVal) Number of item's objects.
Name(BSTR* pName) Item's name.

Here is theIItem Interface

MethodDescription
Value(VARIANT* pValue) (Default method) Return the value of the item.
IsFile(BOOL *pVal) Return TRUE if the Item object is a file otherwise it returns FALSE.
FileName(BSTR *pVal) The name of the uploaded file. AVALIABLE ONLY if object is a file.
FilePath(BSTR *pVal) The path of the uploaded file. AVALIABLE ONLY if object is a file.
FileExt(BSTR *pVal) The extension of the uploaded file. AVALIABLE ONLY if object is a file.
ContentType(BSTR *pVal) The content-type (MIME TYPE) of the uploaded file. AVALIABLE ONLY if object is a file.
TotalBytes(long *pVal) Size in bytes of the uploaded file. AVALIABLE ONLY if object is a file.
GetChunk(long offset, long length, VARIANT* pChunk) Return a chunk of the file with the specified size and position. AVALIABLE ONLY if object is a file.

Further Help

Additional help is supplied with the demo project zip file which includes documentation !

Special thanks to Xicoloko who wrote http://www.codeproject.com/asp/uploader.asp which give me a push to also build my own component but this time (Complete).

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralGetting Error when adding new fields Pin
7-Nov-01 4:25
suss7-Nov-01 4:25 
GeneralRe: Getting Error when adding new fields Pin
Fayez Mutairi7-Nov-01 7:00
Fayez Mutairi7-Nov-01 7:00 
Generalcannot find dll file Pin
5-Nov-01 10:12
suss5-Nov-01 10:12 
GeneralRe: cannot find dll file Pin
Fayez Mutairi6-Nov-01 1:24
Fayez Mutairi6-Nov-01 1:24 
GeneralRe: cannot find dll file Pin
21-Nov-01 9:18
suss21-Nov-01 9:18 
GeneralCan't Register the component! Ugh! Pin
30-Oct-01 17:02
suss30-Oct-01 17:02 
GeneralRe: Can't Register the component! Ugh! Pin
Fayez Mutairi6-Nov-01 1:11
Fayez Mutairi6-Nov-01 1:11 
GeneralRe: Can't Register the component! Ugh! Pin
14-Nov-01 23:48
suss14-Nov-01 23:48 
GeneralRequest on encrypted upload Pin
29-Oct-01 17:05
suss29-Oct-01 17:05 
GeneralRe: Request on encrypted upload Pin
Fayez Mutairi6-Nov-01 1:06
Fayez Mutairi6-Nov-01 1:06 
Questionhow to change it working with vbscript Pin
9-Sep-01 19:44
suss9-Sep-01 19:44 
AnswerRe: how to change it working with vbscript Pin
Fayez Mutairi10-Sep-01 0:46
Fayez Mutairi10-Sep-01 0:46 
GeneralHelp Pin
3-Sep-01 17:36
suss3-Sep-01 17:36 
GeneralRe: Help Pin
Fayez Mutairi4-Sep-01 1:05
Fayez Mutairi4-Sep-01 1:05 
GeneralI am a piece of sh*t. Pin
30-Aug-01 10:27
suss30-Aug-01 10:27 
GeneralRe: I am a piece of sh*t. Pin
Fayez Mutairi30-Aug-01 16:04
Fayez Mutairi30-Aug-01 16:04 
GeneralRe: I am a piece of sh*t. Pin
Christian Graus30-Aug-01 16:15
protectorChristian Graus30-Aug-01 16:15 
GeneralRe: I am a piece of sh*t. Pin
Christian Graus30-Aug-01 16:17
protectorChristian Graus30-Aug-01 16:17 
GeneralRe: I am a piece of sh*t. Pin
Gregg Carlstrom30-Aug-01 16:28
Gregg Carlstrom30-Aug-01 16:28 
GeneralRe: I am a piece of sh*t. Pin
Jon Sagara30-Aug-01 18:42
Jon Sagara30-Aug-01 18:42 
GeneralRe: I am a piece of sh*t. Pin
John Simmon / outlaw programmer31-Aug-01 13:44
John Simmon / outlaw programmer31-Aug-01 13:44 
GeneralRe: I am a piece of sh*t. Pin
Christian Graus31-Aug-01 14:14
protectorChristian Graus31-Aug-01 14:14 
General&#1605;&#1575;&#1588;&#1575;&#1569; &#1575;&#1604;&#1604;&#1607; Pin
Ghazi H. Wadi12-Jun-01 19:19
Ghazi H. Wadi12-Jun-01 19:19 
GeneralRe: &#1605;&#1575;&#1588;&#1575;&#1569; &#1575;&#1604;&#1604;&#1607; Pin
16-Jun-01 9:52
suss16-Jun-01 9:52 
GeneralRe: &#1605;&#1575;&#1588;&#1575;&#1569; &#1575;&#1604;&#1604;&#1607; Pin
30-Aug-01 10:25
suss30-Aug-01 10:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.