|
This article was created as appendix for Upload file using IE+ADO without user interaction - VBS. The "Upload
file" article lets you build multipart/form-data document from source
file. You can send the binary document to any server using several ways.
1. Internet explorer.
"Upload file"
article contains function IEPostBinaryRequest, which uses most common http
object - Internet explorer. The object has one
great characteristic - you can find it on any Windows computer,
so you can use the code on webhosting computers, without installing of another software.
But the object is NOT optimal to use it in such tasks. Positives,
negatives: + Present on each Windows computer + When debugging, you can uncomment
"Visible" line and see results of upload script directly. -
InternetExplorer.Application does not have a method, which lets you wait to end
of request. You must do "do-loop" to wait. - IE takes more memory than
other methods - IE Uses WinInet API, which is not suitable for use in ASP
applications (You can run maximum of two instances per server in destination
URL)
'sends multipart/form-data To the URL using IE
'FormData - binary (VT_UI1 | VT_ARRAY) multipart form data
Function IEPostBinaryRequest(URL, FormData, Boundary)
'Create InternetExplorer
Dim IE: Set IE = CreateObject("InternetExplorer.Application")
'You can uncoment Next line To see form results
'IE.Visible = True
'Send the form data To URL As POST binary request
IE.Navigate URL, , , FormData, _
"Content-Type: multipart/form-data; boundary=" + Boundary + vbCrLf
Do While IE.Busy
Wait 1, "Upload To " & URL
Loop
'Get a result of the script which has received upload
On Error Resume Next
IEPostBinaryRequest = IE.Document.Body.innerHTML
IE.Quit
End Function
Sub Wait(Seconds, Message)
On Error Resume Next
Dim Shell
If IsEmpty(Shell) Then Set Shell = CreateObject("wscript.shell")
Shell.Popup Message, Seconds, "", 64
End Sub
|
2. http request object.
Other free method to send binary
data is "http request" object from MS. As usuall in MS, there are several versions of
http request objects. 1. XMLHTTP. First http request
object from MS, designed to work with VBS/ASP/WSH. This object uses WinInet API
(which is not suitable for use in ASP applications, see IE) 2.
ServerXMLHTTP. This object was created because of problems with XMLHTTP in
server applications. It contains its own http engine, which is safe to use in
server applications. The http engine has some small limitations (I think it does
not enable client certificates, etc.), but the limitations has probably no
impact to usual demands. You can find XMLHTTP/ServerXMLHTTP at http://msdn.microsoft.com in MS XML package as free
download. 3. WinHTTP object. Similar as ServerXMLHTTP You can find the object
at http://msdn.microsoft.com, search for WinHTTP. My
recomendation - use this object, if you can install it. MSXML 4.0 package uses
this library also (see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/xmlsdk/htm/sdk_dependencies_9po5.asp)
These three objects has
almost the same OLE interface, so you can use the same source code:
'sends multipart/form-data To the URL using WinHttprequest/XMLHTTP
'FormData - binary (VT_UI1 | VT_ARRAY) multipart form data
Function WinHTTPPostRequest(URL, FormData, Boundary)
Dim http 'As New MSXML2.XMLHTTP
'Create XMLHTTP/ServerXMLHTTP/WinHttprequest object
'You can use any of these three objects.
Set http = CreateObject("WinHttp.WinHttprequest.5")
'Set http = CreateObject("MSXML2.XMLHTTP")
'Set http = CreateObject("MSXML2.ServerXMLHTTP")
'Open URL As POST request
http.Open "POST", URL, False
'Set Content-Type header
http.setRequestHeader "Content-Type", "multipart/form-data; boundary=" + Boundary
'Send the form data To URL As POST binary request
http.send FormData
'Get a result of the script which has received upload
WinHTTPPostRequest = http.responseText
End Function
|
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.
or
2. Link this sample from you page.
<A
Href="https://www.motobit.com/tips/detpg_post-binary-data-url/"
Title="Two methods to POST binary or
string data to external URL
and read results sent from
a script on the URL."
>Post binary data to URL from WSH/ASP/VBA/VBScript.</A>
|