为了账号安全,请及时绑定邮箱和手机立即绑定

如何使用与无标题不同的名称将文件上传到 google drive api

如何使用与无标题不同的名称将文件上传到 google drive api

ABOUTYOU 2022-12-29 16:38:28
这是我的代码结构,我试图在对象“formData.append”中发送名称,但我没有成功。文档表明:在正文上发送。文档链接: https ://developers.google.com/drive/api/v3/reference/files/create - https://developers.google.com/drive/api/v3/manage-uploads#http_1我得到了这个答案。{ "kind": "drive#file", "id": "1uz_NN-IyoiPzaheAiKIJu6qlB7ZfxIX2", "name": "Untitled", "mimeType": "application/x-www-form-urlencoded" }名称:《无题》- 我会很感激await User.updateMany({},    [{        $set: {            posts: {                $map: {                    input: "$posts",                    as: "post",                    in: {                        $mergeObjects: [                            "$$post",                            {                                "viewed": {                                    $add: [{ $size: "$$post.visitorIps" }, "$$post.viewed"]                                },                                "visitorIps": []                            }                        ]                    }                }            }        }    }])
查看完整描述

1 回答

?
慕神8447489

TA贡献1780条经验 获得超1个赞

这个改装怎么样?

修改点:

  • 似乎不能直接multipart/form-data使用ajax 请求FormData()。所以在这种情况下,需要创建结构multipart/form-data并将其作为数据发送。

    • 在您的脚本中,仅上传文件内容而没有文件元数据。这样,上传的文件就没有文件名了。在这种情况下,需要上传文件内容和文件元数据multipart/form-data

  • 在您的脚本中,有 2 个属性data

当以上几点反映到您的脚本中时,它会变成如下。

修改脚本:

Upload.prototype.doUpload = function () {

    const file = this.file;  // It supposes that "this.file" is the blob.


    const fr = new FileReader();

    fr.readAsDataURL(file);

    fr.onload = function() {

      const boundary = "xxxxxxxxxx";

      let data = "--" + boundary + "\n";

      data += "Content-Type: application/json; charset=UTF-8\n\n";

      data += JSON.stringify({name: "test_file"}) + "\n";

      data += "--" + boundary + "\n";

      data += "Content-Transfer-Encoding: base64\n\n";

      data += fr.result.split(",")[1] + "\n";

      data += "--" + boundary + "--";

      $.ajax({

        type: "POST",

        beforeSend: function(request) {

          request.setRequestHeader("Authorization", "Bearer" + " " + localStorage.getItem("accessToken"));

          request.setRequestHeader("Content-Type", "multipart/related; boundary=" + boundary);

        },

        url: "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart",

        success: function (data) {

            console.log(data);

        },

        error: function (error) {

            console.log(error);

        },

        async: true,

        data: data,

        cache: false,

        processData: false,

        timeout: 60000

      });

    }

}

笔记:

  • 在这个修改后的脚本中,

    • 它假定this.file在您的脚本中是 blob。

    • 您的访问令牌可用于将文件上传到 Google 云端硬盘。

  • 使用uploadType=multipart时,最大文件大小为 5 MB。请注意这一点。


查看完整回答
反对 回复 2022-12-29
  • 1 回答
  • 0 关注
  • 108 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号