Sub Button3_Click
ProgressDialogShow("Uploading File...")
Dim Dir, FN, title, parent As String
Dir = File.DirInternal
FN = "Test.jpg"
title = "EINE TESTDATEI"
parent = ""
Dim boundary As String
boundary = "bound"
Dim In As InputStream = File.OpenInput(Dir, FN)
Dim out2 As OutputStream
out2.InitializeToBytesArray(1000)
'A Map of properties To be set on the File once uploaded.
Dim m As Map
m.Initialize
m.Put("title", title)
If parent <> "" Then
m.Put("parents", parent) '“ID # OF FOLDER To Upload To” remove this line To upload directly To Main Drive Folder.
End If
'The properties must be In JSON.
Dim J As JSONGenerator
J.Initialize(m)
'I use this To determine the proper mimeType For the File being uploaded. I only have 3 mimeTypes below, more can be added.
Dim FileName As String = FN
FileName = FileName.SubString(FileName.LastIndexOf(".")+1)
Dim ContentType As String
Dim Suffix As Object = FileName
Select Suffix
Case "jpg"
ContentType = "image/jpeg"
Case "xls"
ContentType = "application/vnd.ms-excel"
Case "csv"
ContentType = "text/csv"
Case Else
ContentType = "application/octet-stream"
End Select
'This Is where the Syntax has To be pretty much perfect otherwise you’ll get a Bad Request response.
Dim data() As Byte
'The first half of the POST contains the JSON data AND the Content Type from above.
data = ("--" & boundary & CRLF & "Content-Type: application/json; charset=UTF-8" & CRLF & CRLF & J.ToString & CRLF & CRLF & "--" & boundary & CRLF & "Content-Type: " & ContentType & CRLF & CRLF).GetBytes("UTF8")
'Write it To the Stream.
out2.WriteBytes(data, 0, data.Length)
'Add the File itself To the Stream.
File.Copy2(In, out2)
'Add the final boundary to the POST
data = (CRLF & CRLF & "--" & boundary & "--").GetBytes("UTF8")
'Write it To the Stream Then make the Bytes the complete Stream.
out2.WriteBytes(data, 0, data.Length)
data = out2.ToBytesArray
'POST the Bytes and change the Content Type.
'The boundary can be any word you choose.
Upload.Initialize ("FileUpload", Me)
Upload.PostBytes(DriveUploadLink, data)
Upload.GetRequest.SetContentType("multipart/related; boundary=bound")
Upload.GetRequest.SetHeader("Authorization", "Bearer " & AccessToken)
End Sub