I have some code but I can't see why it doesn't work...
Sub UploadJPGWithCURL()
Dim winHttpReq As Object
Dim fileData As String
Dim boundary As String
Dim fileName As String
Dim filePath As String
filePath = "Z:\Desktop\testimage.jpg"
fileName = "testimage.jpg"
boundary = "----------------------------" & Format(Now, "ddmmyyyyhhmmss")
fileData = "--" & boundary & vbCrLf
fileData = fileData & "Content-Disposition: form-data; name=""fileToUpload""; filename=""" & fileName & """" & vbCrLf
fileData = fileData & "Content-Type: image/jpeg" & vbCrLf & vbCrLf
'fileData = fileData & filePath & vbCrLf
fileData = fileData & getBinaryFile(filePath) & vbCrLf
fileData = fileData & "--" & boundary & "--" & vbCrLf
Debug.Print fileData
Set winHttpReq = CreateObject("WinHttp.WinHttpRequest.5.1")
winHttpReq.Open "POST", "[THE URL TO MY WEBSERVER PHP PAGE THAT IS GOING TO PROCESS THE UPLOAD]", False
winHttpReq.setRequestHeader "Content-Type", "multipart/form-data; boundary=" & boundary
winHttpReq.send (fileData)
If winHttpReq.Status = 200 Then
Debug.Print winHttpReq.ResponseText
Else
Debug.Print "Request failed with status code: " & winHttpReq.Status
End If
End Sub
Function getBinaryFile(filePath)
Dim binaryStream
Set binaryStream = CreateObject("ADODB.Stream")
binaryStream.Type = 1
binaryStream.Open
binaryStream.LoadFromFile filePath
getBinaryFile = binaryStream.Read
binaryStream.Close
End Function
On the server, PHP, the $_FILES array is giving an error code 3 for this file, which seems to be partial upload....? (I get this if a Print_r the $_FILES array at the top of the php code....
Array
(
[fileToUpload] => Array
(
[name] => testimage.jpg
[full_path] => testimage.jpg
[type] =>
[tmp_name] =>
[error] => 3
[size] => 0
)
)
Any help or guidance would be appreciated!
Thanks
Chris
You have to convert the binary content of the file and the data sent like this:
Function getBinaryFile(filePath) As Variant
Dim binaryStream As Variant
Set binaryStream = CreateObject("ADODB.Stream")
binaryStream.Type = 1
binaryStream.Open
binaryStream.LoadFromFile filePath
getBinaryFile = StrConv(binaryStream.Read, vbUnicode) ' Convert to Unicode
binaryStream.Close
End Function
And when sending the data:
Create this function:
Function ToByteArray(str As String) As Byte()
ToByteArray = StrConv(str, vbFromUnicode)
End Function
And use it like this:
winHttpReq.send ToByteArray(fileData) ' Convert from Unicode
Tested and it worked:
Array
(
[fileToUpload] => Array
(
[name] => sample.jpg
[type] => image/jpeg
[tmp_name] => J:\tmp\php827.tmp
[error] => 0
[size] => 3287
)
)