German Probleme beim Upload zu Google Drive / HTTP Multipart

mw71

Active Member
Licensed User
Longtime User
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.

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
 

DonManfred

Expert
Licensed User
Longtime User
Na, und was gibt das LOG aus bei diesem Request? Wäre evtl. hilfreich, wenn du den Log dazu postest? ;)
 

mw71

Active Member
Licensed User
Longtime User
ja, kurz gesagt, er gibt die ID, FileType und Namen (unnamed oder so) zurück.
Ich werde es noch mal durchlaufen lassen und das Log ergänzen.

so, hier das Log:
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Getting access token from refresh token...
** Activity (main) Resume **
*** Service (httputils2service) Create ***
** Service (httputils2service) Start **
###### MultipartPost Daten
--*/*
Content-Type: application/json; charset=UTF-8
{"name":"TestFile.txt"}
--*/*
Content-Type: application/octet-stream
ein Text zum Testen
--*/*--
###### Ende
{
"kind": "drive#file",
"id": "0B8MzKRueEMO4TVFuZF93RE1CY1E",
"name": "Untitled",
"mimeType": "application/octet-stream"
}
 
Last edited:

mw71

Active Member
Licensed User
Longtime User
hier noch mal das ungefilterte Log, evtl. kann da jemand was erkennen?
 

Attachments

  • unfilter_log.txt
    108.6 KB · Views: 720

Paulsche

Well-Known Member
Licensed User
Longtime User
Hallo mw71,

ich habe auch das Problem, dass mein Backup in Google Drive nun nicht mehr funktioniert,
Dein Code ist auch das einzige Beispiel das ich finden kann. Hat es bei Dir nun funktioniert
ein File hochzuladen?
 
Top