<!-- Download Links -->
Download demo project - 32 Kb
<!-- Main HTML starts here -->
This article is about file uploading through a COM component (
interface). It uses the
IRequest interface to retrieve the information
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
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
Content-Disposition: form-data; name="UploadFile"; filename="C:\SampleFile.txt"
Content-Disposition: form-data; name="submit1"
Content-Disposition: form-data; name="userName"
How to use it
These are the methods for the
|This method should be called after the component creation. The pIUnk must be the Request interface|
|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|
|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|
|Indicates the allowed extensions. The default value means that all extensions are allowed|
|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.SetAllowedExtensions "*" ' all extensions
objUploader.SetForbiddenExtensions "exe", "com"
nUploadResult = objUploader.UploadFile("uploadFile")
I could have used an ASP component so I would have the
interface. But I had some problems with this in the past, that's why I don't use this
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.