Skip to content

Instantly share code, notes, and snippets.

@mdauphin
Created May 30, 2016 12:33
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdauphin/7c162832238aea0f9001bbd1bef75b86 to your computer and use it in GitHub Desktop.
Save mdauphin/7c162832238aea0f9001bbd1bef75b86 to your computer and use it in GitHub Desktop.
Function UploadFile( url, file, fileType )
Set oXMLHTTP = CreateObject("MSXML2.XMLHTTP")
Dim boundary
boundary = "12345678"
Set objStream = CreateObject("ADODB.Stream")
With objStream
.Type = 1 ' binary
.Open
.LoadFromFile(file)
End With
Set sOut = CreateObject("ADODB.Stream")
With sOut
.Charset = "us-ascii"
.Type = 2 ' Text!
.Open
.WriteText "--" & boundary & vbCrLf
.WriteText "Content-Disposition: form-data; name=""file""; filename="""&file&"""" & vbCrLf
.WriteText "Content-Type: " & fileType & vbCrLf & vbCrLf
End With
Set sOut2 = CreateObject("ADODB.Stream")
With sOut2
.Charset = "us-ascii"
.Type = 2 ' Text
.Open
.WriteText vbCrLf & "--" & boundary & "--" & vbCrLf & vbCrLf
End With
Set sAll = CreateObject("ADODB.Stream")
sAll.Type = 1 'binary
sAll.Open
sOut.Position = 0
sOut.CopyTo sAll
objStream.CopyTo sAll
sOut2.Position = 0
sOut2.CopyTo sAll
oXMLHTTP.Open "POST", url, False
oXMLHTTP.setRequestHeader "Content-Type", "multipart/form-data; boundary=" & boundary
oXMLHTTP.setRequestHeader "Connection", "close"
oXMLHTTP.setRequestHeader "Content-length", sAll.Size
sAll.Position = 0
oXMLHTTP.Send sAll.Read()
MsgBox "Upload-Status: " & oXMLHTTP.statusText
End Function
UploadFile "http://exemple.com/upload.php", "file.zip", "application/x-zip-compressed"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment