Skip to content

Multipart upload file to google drive API #257

Open

Description

@kokosky93

I am meeting with dificulties while using multipartUpload method. I need to send json file to the Google Drive API. I have to use multipart to set metadata to a file like name. Without multipart request file will be with untitled name. I don't know how to set boundaries according to Google Drive API documentations. Anyone can help?

This is my code:

    let documents = knownFolders.documents();
    let folder = documents.getFolder("testFolder");
    let file = folder.getFile(("test") + ".json");

    var xxx = { "thing": "cat", "age": "19" };
    var jsonFile = JSON.stringify(xxx);

    file.writeText(jsonFile)
        .then(result => {

            var bghttp = require("nativescript-background-http");
            var session = bghttp.session("file-upload");

            var request = {
                url: "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart",
                method: "POST",
                headers: {
                    "Content-Type": "multipart/related",
                    "Authorization": `Bearer ${this.token.accessToken}`,
                },
                utf8: true
            };

            var params = [
                { name: "test", value: "I need to send metadata to the file I don't know how" },
                { name: "test", filename: file.path, mimeType: "application/json" }
            ];
            var task = session.multipartUpload(params, request);

I have tried a lot but nothing worked out.

Google API
https://developers.google.com/drive/api/v3/manage-uploads

Activity

  1. lini commented on Dec 16, 2019

    @lini
    Contributor

    Can you try saving your metadata as a json file and uploading it like you do the other file? This way you will be able to specify the content type of the metadata as application/json; charset=UTF-8 as the GDrive API expects.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions