亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 Google Drive API Node js 上傳文件時文件名是“無標題”

使用 Google Drive API Node js 上傳文件時文件名是“無標題”

鳳凰求蠱 2022-12-09 13:58:52
我想將文件上傳到 Google 云端硬盤,但上傳的文件沒有文件名。它作為“無標題”文件上傳。如果可行,請給我一個解決方案,然后我接受你的回答這里有人對此有解決方案嗎?提前致謝。這是我的代碼。userController.uploadToDrive = function(req, res){    token = req.body.token;    console.log(token);    var formData = new FormData();    console.log(token);    var fileMetadata = {        'name': 'all.vcf'      };          formData.append("data",fs.createReadStream('./all.vcf'), "all.vcf");    request({        headers: {                      'Authorization': token,            'Content-Type' :'text/x-vcard',        },        resource: fileMetadata,        uri: 'https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart',        body: formData,         filename:'all.vcf',        method: 'POST'    }, function (err, resp, body) {            if(err){                    console.log(err);        }else{            console.log('resp',body);            res.status(200).send()            fs.readdir('./contacts', function (err, files) {                var removefiles = function (file) {                    fs.unlinkSync('./contacts/' + file)                }                files.forEach(function (file) {                    removefiles(file)                })            })        }    });}它的回應是這樣的:resp { "kind": "drive#file", "id": "1tXu9Fc4sdi-yk8QGGvMJqSgxLXhuXNhQ", "name": "Untitled", "mimeType": "text/x-vcard"}
查看完整描述

1 回答

?
白衣非少年

TA貢獻1155條經驗 獲得超0個贊

我相信你的目標和情況如下。

  • 您想要使用 Drive API 將文件上傳到 Google Drive?multipart/form-data。

  • 您的訪問令牌可用于將文件上傳到 Google 云端硬盤。

我認為在您的情況下,元數據和文件內容不能上傳為multipart/form-data.?這樣,文件元數據就無法反映到上傳的文件中。所以為了實現這一點,我想提出以下修改。

模式 1:

在此模式中,const request = require("request")使用。

修改腳本:

const fs = require("fs");

const request = require("request");



token = req.body.token;

fs.readFile("./all.vcf", function (err, content) {

? if (err) {

? ? console.error(err);

? }

? const metadata = {

? ? name: "all.vcf",

? ? mimeType: "text/x-vcard"

? };

? const boundary = "xxxxxxxxxx";

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

? data += 'Content-Disposition: form-data; name="metadata"\r\n';

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

? data += JSON.stringify(metadata) + "\r\n";

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

? data += 'Content-Disposition: form-data; name="file"\r\n\r\n';

? const payload = Buffer.concat([

? ? Buffer.from(data, "utf8"),

? ? Buffer.from(content, "binary"),

? ? Buffer.from("\r\n--" + boundary + "--\r\n", "utf8"),

? ]);

? request(

? ? {

? ? ? method: "POST",

? ? ? url:

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

? ? ? headers: {

? ? ? ? Authorization: token,

? ? ? ? "Content-Type": "multipart/form-data; boundary=" + boundary,

? ? ? },

? ? ? body: payload,

? ? },

? ? function (err, resp, body) {


? ? ? if(err){

? ? ? ? console.log(err);

? ? ? }else{

? ? ? ? console.log('resp',body);

? ? ? ? res.status(200).send()

? ? ? ? fs.readdir('./contacts', function (err, files) {

? ? ? ? ? ? var removefiles = function (file) {

? ? ? ? ? ? ? ? fs.unlinkSync('./contacts/' + file)

? ? ? ? ? ? }

? ? ? ? ? ? files.forEach(function (file) {

? ? ? ? ? ? ? ? removefiles(file)

? ? ? ? ? ? })

? ? ? ? })

? ? ? }

? ? }

? );

});

模式 2:

在此模式中,使用節點提取。在您的腳本中,new FormData()使用了。所以我認為這種模式可能是你期望的方向。


修改腳本:

const FormData = require("form-data");

const fetch = require("node-fetch");

const fs = require("fs");



token = req.body.token;

var formData = new FormData();

var fileMetadata = {

? name: "all.vcf",

? mimeType: "text/x-vcard",

};

formData.append("metadata", JSON.stringify(fileMetadata), {

? contentType: "application/json",

});

formData.append("data", fs.createReadStream("./all.vcf"), {

? filename: "all.vcf",

});

fetch(

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

? { method: "POST", body: formData, headers: { Authorization: token } }

)

? .then((res) => res.json())

? .then((json) => console.log(json))

查看完整回答
反對 回復 2022-12-09
  • 1 回答
  • 0 關注
  • 114 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號