【问题标题】:How to upload a image to google drive using react native with react-native-drive-api-wrapper?如何使用带有 react-native-drive-api-wrapper 的 react native 将图像上传到谷歌驱动器?
【发布时间】:2020-01-18 05:34:27
【问题描述】:

我是反应原生的新手,并使用 react-native-drive-api-wrapper 在反应原生中与谷歌驱动器一起工作。所以我可以创建文件夹,也可以使用它创建一个包含一些内容的新文件。

  const contents = "My text file contents";
  GDrive.files.createFileMultipart(
       contents,
       "text/plain", {
         parents: ["root"],
         name: "text2.txt"
       },
       false);

但不知道如何将设备中的文件上传到谷歌驱动器。最后,我的目标是将图像和视频上传到驱动器。请帮助我前进。

【问题讨论】:

    标签: android react-native google-drive-api multipartform-data image-uploading


    【解决方案1】:

    我可以使用 base64 将图像上传到谷歌驱动器,这是下面的示例代码。

    fs.readFile("file:///storage/emulated/0/DCIM/Camera/IMG_20190325_111404.jpg",'base64')
       .then((res => {             //conversion of image to base64
           console.log(res);
           GDrive.files.createFileMultipart(
                res,
                "'image/jpg'", {
                parents: ["root"], //or any path
                name: "photo.jpg"
              },
                true)              //make it true because you are passing base64 string otherwise the uploaded file will be not supported
                 .then(a=>{
                console.log(a);
              });
            }
            );
    

    【讨论】:

    • 但是在文件大的情况下,由于内存分配错误,android应用程序崩溃了。
    【解决方案2】:

    您是否尝试过使用react-native-image-picker

    您可以使用它来获取图像,然后将数据发送到谷歌驱动器。

    喜欢

    ImagePicker.launchImageLibrary(options, (response) => {
        if (response.uri) {
            // response.data is the actual image data.
            // response.type can give you the mime for pictures only
            // response.fileName for the name of the file.
            GDrive.files.createFileMultipart(
                response.data,
                response.type, {
                    parents: ["root"],
                    name: response.fileName,
                }, false);
        }
    });
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-31
    • 2020-06-21
    • 1970-01-01
    • 2018-08-25
    • 2022-10-18
    • 1970-01-01
    • 2019-04-19
    • 2021-05-29
    相关资源
    最近更新 更多