【问题标题】:Using Google Drive API v3 to convert .docx or .doc file to google docs file使用 Google Drive API v3 将 .docx 或 .doc 文件转换为 google docs 文件
【发布时间】: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


【解决方案1】:

当我看到你上传application/msword文件的脚本时,我注意到了一个修改点。那么下面的修改呢?在这种情况下,需要转换为流类型。

另外,我确认当我测试buffer.toString()作为media的正文时,上传的文件是无效文件。

修改脚本:

const { Readable } = require("stream");
const stream = new Readable();
stream.push(Buffer.from(data));
stream.push(null);

const driveResponse = drive.files.create({
    requestBody: {
        name: filename,
    },
    media: {
        body: stream
    }
});
driveResponse.then(data => {
    if(data.status == 200)
        res.redirect('/notes');
    else
        console.log("file not uploaded.");
}).catch(err => { throw new Error(err) })

注意:

  • 此修改后的脚本假定您的Buffer.from(data) 值是application/msword 的有效值。所以如果Buffer.from(data)的值为application/msword无效,请重新检查数据。

【讨论】:

  • 嗨@Tanaike,我尝试了您修改后的解决方案,它完美地解决了我的问题。非常感谢您的帮助和时间!
  • @Octopus 感谢您的回复和测试。我很高兴你的问题得到了解决。也谢谢你。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-15
  • 1970-01-01
  • 2019-07-04
  • 1970-01-01
  • 2015-11-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多