Click here to Skip to main content
Licence 
First Posted 6 Aug 2000
Views 185,938
Bookmarked 56 times

File uploading with COM and ASP

By | 6 Aug 2000 | Article
A simple COM component with source that provides file upload capabilities for your ASP pages.
  • Download demo project - 32 Kb
  • Introduction

    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:

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

    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. :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.

    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

    About the Author

    xicoloko

    Architect
    VisionOne AG
    Switzerland Switzerland

    Member

    XicoLoko is a brazilian developer based in Switzerland.


    Sign Up to vote   Poor Excellent
    Add a reason or comment to your vote: x
    Votes of 3 or less require a comment

    Comments and Discussions

     
    You must Sign In to use this message board. (secure sign-in)
     
    Search this forum  
     FAQ
        Noise  Layout  Per page   
      Refresh
    QuestionProcess Pinmemberneetugreat200222222222221:26 21 Aug '06  
    GeneralI cannot download the file demo PinmemberJumahijah15:49 14 Mar '05  
    Generalt problem PinsussAnonymous16:01 25 Apr '04  
    Generalttt PinsussAnonymous7:56 23 May '03  
    GeneralDLL Pinmemberfredclown8:09 9 Apr '03  
    GeneralVirus Scanning Uploads PinmemberAndrewpe9:52 8 Feb '03  
    GeneralRe: Virus Scanning Uploads Pinmemberpunahou198012:46 27 Mar '08  
    QuestionWill it work for binary files? Pinmemberjsanjosem20:47 25 Dec '02  
    Generalhelp!! PinmemberAnonymous5:57 10 May '02  
    GeneralRe: help!! Pinmemberxico6:24 10 May '02  
    GeneralAbout MyStrStr fumction Pinmemberstan_lin18:44 23 Mar '02  
    GeneralRe: About MyStrStr fumction Pinmemberxicoloko13:50 24 Mar '02  
    GeneralRe: About MyStrStr fumction Pinmemberstan_lin19:26 24 Mar '02  
    GeneralRe: About MyStrStr fumction Pinmemberxicoloko23:58 25 Mar '02  
    GeneralRe: SAFEARRAY Problem Pinmemberstan_lin3:04 26 Mar '02  
    GeneralMemory leak PinmemberWout Louwers22:28 18 Nov '01  
    GeneralRe: Memory leak Pinmemberprogramsalon16:22 28 May '04  
    Generallibrary not registered PinmemberAnonymous10:39 25 Jul '01  
    GeneralCompiled DLL Pinmembermathe23:13 11 Apr '01  
    QuestionCan anyone send me the compiled DLL PinmemberEduardo10:37 21 Mar '01  
    AnswerRe: Can anyone send me the compiled DLL Pinmemberliangar20:29 9 Mar '04  
    GeneralRe: Can anyone send me the compiled DLL Pinmemberefwefew21:50 12 Mar '07  
    QuestionCan only run on the compter that compiles it? Pinmembercop2c6:38 12 Jan '01  
    AnswerRe: Can only run on the compter that compiles it? PinmemberAnonymous10:28 19 Jun '01  
    AnswerRe: Can only run on the compter that compiles it? PinsussEgyptain20:22 29 Nov '02  

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

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

    Permalink | Advertise | Privacy | Mobile
    Web02 | 2.5.120517.1 | Last Updated 7 Aug 2000
    Article Copyright 2000 by xicoloko
    Everything else Copyright © CodeProject, 1999-2012
    Terms of Use
    Layout: fixed | fluid