Click here to Skip to main content
12,397,027 members (68,113 online)
Click here to Skip to main content
Add your own
alternative version

Stats

356.7K views
6.6K downloads
61 bookmarked
Posted

Advanced ASP Uploader

, 28 Aug 2001
Rate this:
Please Sign up or sign in to vote.
An advanced COM component that provides file upload capabilities for your ASP pages.
<!-- Add the rest of your HTML here -->

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

Share

About the Author

Fayez Al-Mutairi
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
QuestionThis doesn't work with Webkit based browsers Pin
GPATI17-Jun-13 2:53
memberGPATI17-Jun-13 2:53 
Questionmultipart parsing without the upload/post part Pin
Luka9-Jan-12 4:33
memberLuka9-Jan-12 4:33 
Questionhow to register myrequest.dll on SQL Server 2005 or Vista Home Basic Pin
Mekoder20-Sep-07 0:52
memberMekoder20-Sep-07 0:52 
AnswerRe: how to register myrequest.dll on SQL Server 2005 or Vista Home Basic Pin
pawandhir24-Jun-09 5:50
memberpawandhir24-Jun-09 5:50 
GeneralProblem compiling Pin
jerry Scannell6-Nov-06 14:38
memberjerry Scannell6-Nov-06 14:38 
GeneralMyRequest.Form init error Pin
choijung7120-Jul-06 22:42
memberchoijung7120-Jul-06 22:42 
GeneralWrite file error Pin
LienKim10-Feb-06 16:25
memberLienKim10-Feb-06 16:25 
GeneralRe: Write file error Pin
Visible Soul30-Mar-06 0:42
memberVisible Soul30-Mar-06 0:42 
GeneralError "Server.Object" Pin
Shefa15-Jan-06 5:37
memberShefa15-Jan-06 5:37 
GeneralRe: Error "Server.Object" Pin
Adhan8-Feb-06 22:35
memberAdhan8-Feb-06 22:35 
GeneralSelect/Options - How does one handle them Pin
dougcranston5-Dec-05 5:41
memberdougcranston5-Dec-05 5:41 
GeneralConvert JScript example to VBScript Pin
bbutler18-Aug-05 7:14
memberbbutler18-Aug-05 7:14 
GeneralRe: Convert JScript example to VBScript Pin
bbutler18-Aug-05 8:32
memberbbutler18-Aug-05 8:32 
GeneralRe: Convert JScript example to VBScript Pin
bbutler18-Aug-05 10:22
memberbbutler18-Aug-05 10:22 
GeneralRe: Convert JScript example to VBScript Pin
bbutler18-Aug-05 10:57
memberbbutler18-Aug-05 10:57 
GeneralError while creating object of MyRequest.DLL Pin
Tejal Metha15-Jun-05 23:35
memberTejal Metha15-Jun-05 23:35 
GeneralRe: Error while creating object of MyRequest.DLL Pin
pawandhir24-Jun-09 5:52
memberpawandhir24-Jun-09 5:52 
QuestionHow to open file from database? Pin
knight_zhuge5-Aug-04 16:15
memberknight_zhuge5-Aug-04 16:15 
General'adTypeBinary' is undefined Pin
Vexez14-Jun-04 17:12
memberVexez14-Jun-04 17:12 
Generalerror : Det virker ikke en skid Pin
madspaaskesen25-Jan-04 7:18
membermadspaaskesen25-Jan-04 7:18 
GeneralRe: error : Det virker ikke en skid Pin
sketch18-Nov-04 4:22
susssketch18-Nov-04 4:22 
GeneralProblem solved Pin
Sketch24-Nov-04 8:17
memberSketch24-Nov-04 8:17 
GeneralFile upload error Pin
Popeye Doyle Murray14-Aug-03 7:47
memberPopeye Doyle Murray14-Aug-03 7:47 
GeneralRe: File upload error Pin
repressedRadar15-Aug-07 5:09
memberrepressedRadar15-Aug-07 5:09 
Generalwriting data to client; filenames.extensions are LOST Pin
AdyRocker13-Jul-03 7:56
memberAdyRocker13-Jul-03 7:56 
GeneralError when show large files &gt; 65Kb Pin
Nicolay Ch28-Jun-03 23:46
memberNicolay Ch28-Jun-03 23:46 
GeneralClear Text File Pin
JForck23-Oct-02 5:05
sussJForck23-Oct-02 5:05 
GeneralRe: Clear Text File Pin
Pete Bassett23-Oct-02 5:32
memberPete Bassett23-Oct-02 5:32 
GeneralRe: Clear Text File Pin
JForck23-Oct-02 11:55
memberJForck23-Oct-02 11:55 
GeneralRe: Clear Text File Pin
Pete Bassett24-Oct-02 0:13
memberPete Bassett24-Oct-02 0:13 
GeneralRe: Clear Text File Pin
JForck24-Oct-02 7:10
sussJForck24-Oct-02 7:10 
GeneralRe: Clear Text File Pin
Tony Truong2-Feb-03 23:49
memberTony Truong2-Feb-03 23:49 
GeneralRe: Clear Text File Pin
Daniel Turini23-Oct-02 5:39
memberDaniel Turini23-Oct-02 5:39 
GeneralRe: Clear Text File Pin
JForck23-Oct-02 11:52
sussJForck23-Oct-02 11:52 
GeneraladTypeBinary not defined Pin
jdiana25-Sep-02 8:31
sussjdiana25-Sep-02 8:31 
GeneralRe: adTypeBinary not defined Pin
ersan ersoy1-Oct-04 6:27
sussersan ersoy1-Oct-04 6:27 
Questioncan i use chinese filename Pin
Anonymous10-Apr-02 17:04
memberAnonymous10-Apr-02 17:04 
GeneralUnable to create object of ADODB.Stream Pin
Statbat22-Mar-02 0:31
memberStatbat22-Mar-02 0:31 
GeneralRe: Unable to create object of ADODB.Stream Pin
Anonymous24-Mar-02 17:00
memberAnonymous24-Mar-02 17:00 
Generalfiles stored in memory Pin
sunisha19-Mar-02 10:35
membersunisha19-Mar-02 10:35 
GeneralRe: files stored in memory Pin
<marquee>Al-Mutairi, Fayez</marquee>6-Apr-02 21:40
memberAl-Mutairi, Fayez6-Apr-02 21:40 
Generalpurpose of various functions in upload.inc Pin
Anonymous18-Feb-02 8:32
memberAnonymous18-Feb-02 8:32 
GeneralError While Uploading Pin
Dhaval_Parikh10-Jan-02 21:55
memberDhaval_Parikh10-Jan-02 21:55 
GeneralRe: Error While Uploading Pin
tranvan16-Jan-02 3:34
membertranvan16-Jan-02 3:34 
GeneralASP KAMI Pin
kamranali10-Jan-02 17:39
memberkamranali10-Jan-02 17:39 
QuestionUse component in WINNT 4.0 and IIS 4.0 ? Pin
tranvan31-Dec-01 5:42
membertranvan31-Dec-01 5:42 
AnswerRe: Use component in WINNT 4.0 and IIS 4.0 ? Pin
Gabriel Paradelo8-Aug-03 11:20
memberGabriel Paradelo8-Aug-03 11:20 
GeneralError with checkbox control Pin
tranvan31-Dec-01 5:34
membertranvan31-Dec-01 5:34 
QuestionOpera compatibility? Pin
Dave Hiseman7-Dec-01 1:08
memberDave Hiseman7-Dec-01 1:08 
GeneralGetting Error when adding new fields Pin
darend7-Nov-01 4:25
memberdarend7-Nov-01 4: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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160721.1 | Last Updated 29 Aug 2001
Article Copyright 2001 by Fayez Al-Mutairi
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid