简体   繁体   中英

Using Google Drive API v3 to convert .docx or .doc file to google docs file

I need to upload a.doc or.docx file to google drive using Google Drive API v3 then convert it also using Google Drive API v3 so that the content of the file will be readable and editable in my own web application (hosted in node.js). I have tried to do this using drive.files.copy, however, I kept on receiving the error: The API returned an error: Error: Conversion of the uploaded content to the requested output type is not supported. Can anyone help me in figuring out how to do this? Any help will be greatly appreciated. Thank you!

Here is the code that I used for converting, however, it is not working:

                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.');
                    }
                });

Here is the code I used for uploading the file:

            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) })

When I saw your script for uploading the file of application/msword , I noticed a modification point. So how about the following modification? In this case, it is required to convert to the stream type.

Also, I confirmed that when I tested buffer.toString() as the body of media , the uploaded file was an invalid file.

Modified script:

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) })

Note:

  • This modified script supposes that your value of Buffer.from(data) is the valid value as application/msword . So if the value of Buffer.from(data) is invalid as application/msword , please check the data again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM