Click here to Skip to main content
11,934,783 members (34,831 online)
Click here to Skip to main content
Add your own
alternative version


57 bookmarked

File uploading with COM and ASP

, 6 Aug 2000
Rate this:
Please Sign up or sign in to vote.
A simple COM component with source that provides file upload capabilities for your ASP pages.
<!-- Download Links -->
  • Download demo project - 32 Kb
  • <!-- Main HTML starts here -->


    This article is about file uploading through a COM component (IUploader interface). It uses the IRequest interface to retrieve the information needed.

    There are a bunch of free and comercial solutions, but I wanted to make mine with an easy approach.

    Since you cannot use the request object when you are uploading a file, I created the method GetFormValue to retrieve any variables from a form. You pass the variable name (case sensitive) and it returns the value found.

    To accomplish the file upload and to retrieve any form variable is needed to get the whole buffer from the request object and interpret it. This a basic sample of this buffer:

    Content-Disposition: form-data; name="UploadFile"; filename="C:\SampleFile.txt"
    Content-Type: text/plain
    Test file...
    Content-Disposition: form-data; name="submit1"
    Content-Disposition: form-data; name="userName"

    How to use it

    These are the methods for the IUploader interface:

    Method Description
    StartUpload(IUnknown* pIUnk) This method should be called after the component creation. The pIUnk must be the Request interface
    SetDestinationPath(BSTR bsPath) This method must be called to define where the file will be saved. The directory will be created if it doesn't exists
    GetError(long lError, BSTR* pbsReturn) This method returns the error message for a failed upload. The lError is the return value of a call to UploadFile
    SetMaxFileSize(long lSize) Defines the largest file size to be uploaded (bytes). If lSize is -1 there's no restriction
    GetFormValue(BSTR bsFieldName, BSTR* pbsReturn) Gets the value of a form variable
    UploadFile(BSTR bsFieldName, long* plResult) Uploads a file. If lResult is equal 0 the operation succeeded
    SetAllowedExtensions(SAFEARRAY(VARIANT) FileExtensions) Indicates the allowed extensions. The default value means that all extensions are allowed
    SetForbiddenExtensions(SAFEARRAY(VARIANT) FileExtensions) Defines the forbidden file extensions
    GetUploadFilename(BSTR bsField, BSTR* pbsFilename) Returns the filename of an upload file

    The following VBScript code uploads a file using my component:

    const UploadOK               = 0
    const FilesizeInvalid        = 1
    const ExtensionNotAllowed    = 2
    const DestinationPathInvalid = 3
    const UnknownError           = 4
    const UnableToCreateFile     = 5
    const FileDoesntExist        = 6
    Set objUploader = Server.CreateObject("InetUtil.Uploader") 
    objUploader.StartUpload(Request)    'passing the IRequest interface for the component
    strUserName = objUploader.GetFormValue("userName")
    objUploader.SetDestinationPath "c:\temp"
    objUploader.SetAllowedExtensions "*"  ' all extensions
    objUploader.SetForbiddenExtensions "exe", "com"
    nUploadResult = objUploader.UploadFile("uploadFile")

    I could have used an ASP component so I would have the IRequest interface. But I had some problems with this in the past, that's why I don't use this approach. Blush | :O )

    WARNING: There's two known memory leaks that I don't know from where they come. I think it's something about the bad use of COleSafeArray. If you find it please submit.

    In order to use the demo you must have an Win 2K or NT Machine (IIS) because the COM server is an out-of-process. To use it in a Win95/98 machine (PWS) you should create a new in-proc component and then copy the implementation.


    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


    About the Author

    Architect VisionOne AG
    Switzerland Switzerland
    XicoLoko is a brazilian developer based in Switzerland.

    You may also be interested in...

    Comments and Discussions

    QuestionProcess Pin
    neetugreat200222222222221-Aug-06 22:26
    memberneetugreat200222222222221-Aug-06 22:26 
    GeneralI cannot download the file demo Pin
    Jumahijah14-Mar-05 16:49
    memberJumahijah14-Mar-05 16:49 
    Generalt problem Pin
    Anonymous25-Apr-04 17:01
    sussAnonymous25-Apr-04 17:01 
    Generalttt Pin
    Anonymous23-May-03 8:56
    sussAnonymous23-May-03 8:56 
    GeneralDLL Pin
    fredclown9-Apr-03 9:09
    memberfredclown9-Apr-03 9:09 
    GeneralVirus Scanning Uploads Pin
    Andrewpe8-Feb-03 10:52
    memberAndrewpe8-Feb-03 10:52 
    GeneralRe: Virus Scanning Uploads Pin
    punahou198027-Mar-08 13:46
    memberpunahou198027-Mar-08 13:46 
    QuestionWill it work for binary files? Pin
    jsanjosem25-Dec-02 21:47
    memberjsanjosem25-Dec-02 21:47 
    Generalhelp!! Pin
    Anonymous10-May-02 6:57
    memberAnonymous10-May-02 6:57 
    GeneralRe: help!! Pin
    xico10-May-02 7:24
    memberxico10-May-02 7:24 
    GeneralAbout MyStrStr fumction Pin
    stan_lin23-Mar-02 19:44
    memberstan_lin23-Mar-02 19:44 
    GeneralRe: About MyStrStr fumction Pin
    xicoloko24-Mar-02 14:50
    memberxicoloko24-Mar-02 14:50 
    GeneralRe: About MyStrStr fumction Pin
    stan_lin24-Mar-02 20:26
    memberstan_lin24-Mar-02 20:26 
    GeneralRe: About MyStrStr fumction Pin
    xicoloko26-Mar-02 0:58
    memberxicoloko26-Mar-02 0:58 
    GeneralRe: SAFEARRAY Problem Pin
    stan_lin26-Mar-02 4:04
    memberstan_lin26-Mar-02 4:04 
    GeneralMemory leak Pin
    Wout Louwers18-Nov-01 23:28
    memberWout Louwers18-Nov-01 23:28 
    GeneralRe: Memory leak Pin
    programsalon28-May-04 17:22
    memberprogramsalon28-May-04 17:22 
    Generallibrary not registered Pin
    Anonymous25-Jul-01 11:39
    memberAnonymous25-Jul-01 11:39 
    GeneralCompiled DLL Pin
    mathe12-Apr-01 0:13
    membermathe12-Apr-01 0:13 
    QuestionCan anyone send me the compiled DLL Pin
    Eduardo21-Mar-01 11:37
    memberEduardo21-Mar-01 11:37 
    AnswerRe: Can anyone send me the compiled DLL Pin
    liangar9-Mar-04 21:29
    memberliangar9-Mar-04 21:29 
    GeneralRe: Can anyone send me the compiled DLL Pin
    efwefew12-Mar-07 22:50
    memberefwefew12-Mar-07 22:50 
    QuestionCan only run on the compter that compiles it? Pin
    cop2c12-Jan-01 7:38
    membercop2c12-Jan-01 7:38 
    AnswerRe: Can only run on the compter that compiles it? Pin
    Anonymous19-Jun-01 11:28
    memberAnonymous19-Jun-01 11:28 
    AnswerRe: Can only run on the compter that compiles it? Pin
    Egyptain29-Nov-02 21:22
    sussEgyptain29-Nov-02 21:22 
    AnswerNo, only on the computers where you register it Pin
    Vagif Abilov25-Dec-02 22:16
    memberVagif Abilov25-Dec-02 22:16 
    GeneralFile Date Check Pin
    Kevin Humphreys12-Sep-00 2:05
    sussKevin Humphreys12-Sep-00 2:05 
    Generali like it, BUT Pin
    mike dalton18-Aug-00 20:11
    sussmike dalton18-Aug-00 20:11 
    GeneralRe: i like it, BUT Pin
    David Cunningham17-Sep-00 18:51
    sussDavid Cunningham17-Sep-00 18:51 
    GeneralRe: i like it, BUT Pin
    TheOne1-Feb-02 15:03
    memberTheOne1-Feb-02 15:03 
    General:(( Not so free Pin
    Anonymous13-Oct-02 0:18
    sussAnonymous13-Oct-02 0:18 
    GeneralRe: :(( Not so free Pin
    David Cunningham14-Oct-02 20:07
    adminDavid Cunningham14-Oct-02 20:07 
    GeneralRe: i like it, BUT Pin
    Anonymous9-Aug-01 4:07
    memberAnonymous9-Aug-01 4:07 
    GeneralRe: i like it, BUT Pin
    dakoen15-Jan-03 22:27
    memberdakoen15-Jan-03 22:27 
    GeneralNot so strong parsing algorithm... Pin
    Ren-Ji Liao8-Aug-00 18:36
    sussRen-Ji Liao8-Aug-00 18:36 

    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
    Web01 | 2.8.151126.1 | Last Updated 7 Aug 2000
    Article Copyright 2000 by xicoloko
    Everything else Copyright © CodeProject, 1999-2015
    Layout: fixed | fluid