The previous article was about uploading files using VBA and IE. But what to do if you have only VBS (in WSH, IE or ASP)? VBS does not have functions which can work with binary data so you cannot read contents of a file from disk.
But you can use several objects which lets you work with binary data (one of them is also on this site, ScriptUtilities). ADODB, which is included in a free microsoft data access sofware (MDAC), enables the work with binary data also.
You can use ScriptUtils.ASPForm to accept uploaded files in ASP. ScriptUtils.ASPForm contains hi-performance, low resources consumption algorithm which can accept up to 2GB of data.
1. There are some steps to upload file using http and multipart/form-data document. First of all we have to read file from a disk. We can use Scripting.FileSystemObject to read text data, or ADODB.Stream to read any file. The GetFile function does the work using ADODB.Stream.
2. The second task we need to complette is a build of multipart/form-data document. The document contains from several fields separated by boundary. Each of the fields has its own header, which contains information about field name, file name and content-type of the source file. ADO Recordset object has a great method AppendChunk, which lets you join parts of multipart/form-data document (open boundary + headers + file contents + close boundary). You can see the code in BuildFormData function.
3. Last task is send the multipart/form-data document as a post request to server with multipart/form-data Content-Type header. We can use at least two object to send POST request - XMLHttp or InternetExplorer. This script uses Navigate method of InternetExplorer.Application object. You can see the code in IEPostBinaryRequest function
Next vbs code is complette vbs file which lets you upload any file from client computer to server using http protocol and multipart/form-data form (form with input type=file). You can copy the code to file named fupload.vbs. Then you can use:
use [cscript|wscript] fupload.vbs file url [fieldname] file ... Local file to upload url ... URL which can accept uploaded data fieldname ... Name of the source form field.UploadFile function can be also used in any other vbs envinronment, for example in ASP application, WSH, or in HTA application. I used Internet Explorer to send binary data (Function IEPostBinaryRequest), because Internet Explorer is installed on each computer with Windows. But remember, that using of the object is NOT ideal. See Post binary data to URL from WSH/ASP/VBA/VBS article. The article contains some better alternative objects, which lets you send binary data to remote script.
for 'Automatic file upload using IE+ADO without user interaction - VBSscript' articleDownload multiple files in one http requestThis article shows a way to download multiple files in one http request. It let's you send an HTML page along with image preview, prepare more files for download and send the files as one data stream. One request, one authentication per multiple files.
Post binary data to URL from WSH/ASP/VBA/VBScript.Two methods to POST binary or string data to external URL and read results sent from a script on the URL.
Automated file upload using IE, without user interaction - VBALets you send file from a client to www server over http connection using IE. The file is sent as a result of type=file form field.
Copyright and use this code
The source code on this page and other samples at https://www.motobit.com/tips/ are a free code, you can use it as you want: copy it, modify it, use it in your products, ...
If you use this code, please:
1. Leave the author note in the source.
2. Link this sample from you page.
<A Href="https://www.motobit.com/tips/detpg_uploadvbsie/" Title="Lets you upload file from a client to www server over http connection using vbs, IE and ADODB. The file is sent as a result of type=file form field encoded multipart/form-data." >Automatic file upload using IE+ADO without user interaction - VBSscript</A>
Do you like it? Rate me on LinkedIn
© 1996 - 2019 Antonin Foller, Motobit Software | About, Contacts