Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

248
Views
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) })
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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.
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!