Hallo,
ich habe ein Problem ein File (zum Testen ein txt File, später SQLite) zum Google Drive zu laden.
Folgender Code lädt das File hoch, aber das File wird als untiteld gespeichert.
Den Code habe ich aus der libGoogleDrive entnommen und angepasst.
Ich hoffe ihr könnt mir helfen, Danke
ich habe ein Problem ein File (zum Testen ein txt File, später SQLite) zum Google Drive zu laden.
Folgender Code lädt das File hoch, aber das File wird als untiteld gespeichert.
Den Code habe ich aus der libGoogleDrive entnommen und angepasst.
B4X:
'Upload a file to Google Drive
'Name - The Name of the filename (Name at Google Drive)
'UploadDir - The (local) folder of the upload file.
'UploadFile - Filename of the upload file
'FileID - The file ID on Google Drive. If not known, a new file will created.
'
Sub UploadFile(Name As String, LocalDir As String, LocalFilename As String, DriveFolder As String, FileId As String)
Dim boundary As String = "*/*"
Dim ContentType As String = "application/octet-stream"
Dim data() As Byte
If Name = "" Then Name = LocalFilename
Try
'Create the stream
Dim In As InputStream = File.OpenInput(LocalDir, LocalFilename)
Dim Jg As JSONGenerator
Dim out2 As OutputStream
Dim EOL As String= Chr(13) & Chr(10)
out2.InitializeToBytesArray(1000)
Dim m As Map
m.Initialize
m.Put("name", Name)
Jg.Initialize(m)
Dim ToSt As String
If DriveFolder <> "" Then
Dim ss As String = """"
ToSt = ss & "parents" & ss & ":"
ToSt = ToSt & "[{" & ss & "kind" & ss & ":" & ss & "drive#fileLink" & ss & "," & ss & "id" & ss & ":" & ss & DriveFolder & ss & "}]}"
ToSt = Jg.ToString.Replace("}",",") & ToSt
Else
ToSt = Jg.ToString
End If
'The first half of the POST contains the JSON data AND the Content Type from above.
Dim RR As String = "--" & boundary & EOL & "Content-Type: application/json; charset=UTF-8" & EOL & EOL & ToSt & EOL & EOL & "--" & boundary & EOL & "Content-Type: " & ContentType & EOL & EOL
data = (RR).GetBytes("UTF-8")
out2.WriteBytes(data, 0, data.Length) 'Write it To the Stream.
File.Copy2(In, out2) 'Add the File itself To the Stream.
data = (EOL & EOL & "--" & boundary & "--").GetBytes("UTF-8") 'Add the final boundary to the POST
out2.WriteBytes(data, 0, data.Length) 'Write it To the Stream Then make the Bytes the complete Stream.
data = out2.ToBytesArray
Log("###### MultipartPost Daten")
Log(BytesToString(data,0,data.Length,"UTF-8"))
Log("###### Ende")
Dim length As Int = File.Size(LocalDir, LocalFilename)
Dim h As HttpJob
h.Initialize("",Me)
If FileId = "" Then
' Upload as new without file id in the URL
h.PostBytes("https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart", data)
h.GetRequest.SetContentType("multipart/related; boundary=" & boundary)
h.GetRequest.SetHeader("Authorization", "Bearer " & myToken)
h.GetRequest.SetHeader("Content-Length",length)
Else
'Create a new stream
Dim OutUpdate As OutputStream
Dim InUpate As InputStream = File.OpenInput(LocalDir, LocalFilename)
OutUpdate.InitializeToBytesArray(1000) 'Initialize the stream
File.Copy2(InUpate, OutUpdate) 'Copy the filestream in the output stream
data = OutUpdate.ToBytesArray 'Fill export data array
'Here is the difference. An update must make with Put, not Post
h.PutBytes("https://www.googleapis.com/upload/drive/v3/files" & "/" & FileId, data)
h.GetRequest.SetContentType("multipart/related; boundary=" & boundary)
h.GetRequest.SetHeader("Authorization", "Bearer " & myToken)
End If
wait For (h) JobDone(h As HttpJob)
If h.Success Then
Log(h.GetString)
End If
Catch
Log("Upload File fail")
Log(LastException.Message)
End Try
Ich hoffe ihr könnt mir helfen, Danke