I want to automate via Visual Basic Script the uploading of files I have in an HTML form.
I am trying to make a post on an html form where I have to upload a file. I have managed to make the http call with this code:
sFile = sFolder & "file.txt"
sBoundary = "-ooo-"
Set req = CreateObject("MSXML2.XMLHTTP")
req.open "POST", SIGNurl, False
req.setRequestHeader "Content-Type", "multipart/form-data; boundary=" + sBoundary
inByteArray = readBytes(sFile)
base64Encoded = encodeBase64(inByteArray)
request = request & sBoundary & vbCrLf
request = request & "Content-Disposition: form-data; name=file; filename=" & sFile & vbCrLf
request = request & "Content-Type: application/x-object" & vbCrLf
request = request & base64Encoded & vbCrLf
request = request & sBoundary & vbCrLf
req.setRequestHeader "Content-Length", Len(request)
req.send request
WScript.Echo req.responseText
Set req = Nothing
Private function readBytes(file)
dim inStream
' ADODB stream object used
set inStream = WScript.CreateObject("ADODB.Stream")
' open with no arguments makes the stream an empty container
inStream.Open
inStream.type= 1
inStream.LoadFromFile(file)
readBytes = inStream.Read()
end function
Private function encodeBase64(bytes)
dim DM, EL
Set DM = CreateObject("Microsoft.XMLDOM")
' Create temporary node with Base64 data type
Set EL = DM.createElement("tmp")
EL.DataType = "bin.base64"
' Set bytes, get encoded String
EL.NodeTypedValue = bytes
encodeBase64 = EL.Text
end function
I am getting a 400 Server error saying "Missing file content in upload". I am not sure what i am missing but something there must be wrong.
Any help?
You are trying to upload binary file content as base64 encoded text, so it's necessary to specify the appropriate MIME header, here is the fixed snippet of your code:
Also you can upload a binary content of the file, here is the simplest example:
I know this is an old question, but I thought others might get some use out of my solution.
I needed to post multiple files as well as standard field data from vbscript and I couldn't find anything that did what I needed, so I took omegastripes answer and extended it to allow me to post arbitrary form data from vbscript.