使用 Google Drive API v3 将 .docx 或 .doc 文件转换为 google docs 文件

Oct*_*pus 6 javascript google-api node.js google-drive-api

我需要使用 Google Drive API v3 将 .doc 或 .docx 文件上传到 google Drive,然后也使用 Google Drive API v3 进行转换,以便文件的内容在我自己的 Web 应用程序(托管在 Node.js 中)中可读和可编辑。 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.');
                    }
                });
Run Code Online (Sandbox Code Playgroud)

这是我用于上传文件的代码:

            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) })
Run Code Online (Sandbox Code Playgroud)

Tan*_*ike 3

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

另外,我确认当我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) })
Run Code Online (Sandbox Code Playgroud)

笔记:

  • 此修改后的脚本假设您的值Buffer.from(data)是有效值application/msword。因此,如果 的值Buffer.from(data)无效application/msword,请再次检查数据。