AutoIT‎ > ‎

HTTP Binary Image File Upload with Autoit

posted Feb 25, 2014, 1:33 PM by Chris G   [ updated Mar 20, 2014, 11:29 AM ]
AutoIT Code Sample - Using an Http POST request to upload any file (JPEG/JPG/PNG/PDF/etc.) to a web service.

;===============================================================================
; Description:		HTTP Binary Image File Upload
; Author(s):		Videre Research, LLC - http://videreresearch.com
;===============================================================================

$sFilePath = @ScriptDir & '\myImage.jpeg' 
Local $sFile = FileOpen($sFilePath, 16)
If $sFile = -1 Then
	MsgBox($MB_SYSTEMMODAL, "", "An error occurred when reading the file.")
EndIf
$sFileRead = BinaryToString(FileRead($sFile))
FileClose($sFile)

$sBoundary = "mymultipartboundary" 

$sPD = '--' & $sBoundary & @CRLF & _
		'Content-Type: application/json; charset=UTF-8' & @CRLF & @CRLF & _
		'{ "text": "JSON Text }' & @CRLF & _
		'--' & $sBoundary & @CRLF & _
		'Content-Type: image/jpeg' & @CRLF & _
		'Content-Transfer-Encoding: binary' & @CRLF & @CRLF & _
		$sFileRead & @CRLF & '--' & $sBoundary & '--' & @CRLF

$oHTTP = ObjCreate("winhttp.winhttprequest.5.1")
$oHTTP.Open("POST", "http://www.yourservice.com/aaa", False)
$oHTTP.SetRequestHeader("Content-Type", 'multipart/related; boundary="' & $sBoundary & '"')
$oHTTP.Send(StringToBinary($sPD))
$oReceived = $oHTTP.ResponseText
$oStatusCode = $oHTTP.Status

ConsoleWrite($oStatusCode & @CRLF)
ConsoleWrite($oReceived)
Comments