【发布时间】:2022-01-13 22:19:19
【问题描述】:
我需要使用 Google Drive API v3 将 .doc 或 .docx 文件上传到 Google Drive,然后也使用 Google Drive API v3 将其转换,以便文件的内容在我自己的 Web 应用程序(托管在 node.js 中)。我尝试使用 drive.files.copy 执行此操作,但是,我一直收到错误消息:API 返回错误:错误:不支持将上传的内容转换为请求的输出类型。谁能帮我弄清楚如何做到这一点?任何帮助将不胜感激。谢谢!
这是我用来转换的代码,但是它不起作用:
drive.files.list({
q: "mimeType = 'application/msword'"
pageSize: 100,
fields: 'nextPageToken, files(id, name)',
}, (err, res) => {
if (err) return console.log('The API returned an error: ' + err);
const files = res.data.files;
if (files.length) {
console.log('Files:');
files.map((file) => {
let result = (file.name).substring(0, (file.name).indexOf('.'));
console.log(file);
console.log(`${file.name} (${file.id})`);
drive.files.copy(
{
fileId: file.id,
requestBody: {
name: result,
mimeType: 'application/vnd.google-apps.document'
}
},
(err, res) => {
if (err) return console.log("The API returned an error: " + err);
console.log(res.data);
}
);
});
}else {
console.log('No files found.');
}
});
这是我用于上传文件的代码:
const driveResponse = drive.files.create({
requestBody: {
name: filename,
mimeType: mimetype
},
media: {
mimeType: mimetype,
body: Buffer.from(data).toString()
}
});
driveResponse.then(data => {
if(data.status == 200)
res.redirect('/notes');
else
console.log("file not uploaded.");
}).catch(err => { throw new Error(err) })
【问题讨论】:
-
我认为
application/msword的文件可以转换成application/vnd.google-apps.document。那请问The API returned an error: Error: Conversion of the uploaded content to the requested output type is not supported错误发生时文件的详细信息? -
@Tanaike 这是我上传的文件的确切内容:Ahello Bhello Chello 2010 年 12 月 3 日 我检查了上传到谷歌驱动器的文件,我也无法在谷歌驱动器中打开它 我编辑了包含我用于上传文件的代码的问题,我在上传文件时使用的代码是否有问题导致错误发生?
-
感谢您的回复。如果您下载了
application/msword文件,但无法打开,则认为该文件无效,该文件为application/msword。这个怎么样? -
@Tanaike 我尝试通过在 console.log 中打印出来来检查它的 mimeType,它指出 mimeType(.doc 文件)是“应用程序/msword”。我昨天尝试使用 mimeType 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' 上传 .docx 文件,当我尝试将其转换为 google docs 时,也会出现同样的错误。
-
感谢您的回复。我不得不为我糟糕的英语水平道歉。从你的回复看,我看不懂
If you download the application/msword file and you cannot open it, it is considered that the file is invalid the file as application/msword. How about this?的回答。
标签: javascript node.js google-api google-drive-api