question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to upload file type in nodejs googleapis (in express multer)

See original GitHub issue

I have a problem in uploding pdf file from from fontend in Nodejs so the problem is how to convert file type to correct form to upload to google drive

my file type is like this

{ fieldname: ‘file’, originalname: ‘test.pdf’, encoding: ‘7bit’, mimetype: ‘application/pdf’, buffer: <Buffer 25 50 44 46 2d 31 2e 34 0a 25 20 e2 e3 cf d3 0a 34 0a 30 0a 6f 62 6a 0a 3c 3c 0a 2f 54 79 70 65 0a 2f 43 61 74 61 6c 6f 67 0a 2f 4e 61 6d 65 73 0a 3c … >, size: 150251 }

and my code is like this ` const {google} = require(‘googleapis’) const drive = google.drive(‘v3’)

const app = express()
const upload = multer()

    app.post(
      'test-upload-pdf-file',
      upload.single('file'),
      (req, res, next) => {
        drive.files.create({
           resource: { mimeType: 'application/pdf'},
           media: {mimeType: 'application/pdf', body: req.file.buffer},
           fields: 'id'
       }, function (err, file) {

      }
    )`

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:30 (1 by maintainers)

github_iconTop GitHub Comments

18reactions
vishald123commented, Apr 9, 2019

Hi, @Thammawat I did it the following way. It worked for me. Let me know if it works for you.

app.post('/upload', upload.single('my file'), (req, res, next) => {
    let stream = require('stream');
    let fileObject = req.file;
    let bufferStream = new stream.PassThrough();
    bufferStream.end(fileObject.buffer);
    google.drive({ version: 'v3', auth: oauth2Client })
        .files.create({
            media: {
                mimeType: 'application/pdf',
                body: bufferStream
            },
            'requestBody': {
                'name': 'test.pdf',
                 mimeType: 'application/pdf',
            },
            fields: 'id',
        }).then(function (resp) {
            console.log(resp,'resp');
        }).catch(function (error) {
            console.log(error);
        })
    res.send(`File uploaded`);
});

1reaction
hiteshsalavicommented, Jul 9, 2019

Good to know. Keep up doing Interesting things!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Upload Files to Google Drive with Node.js, Express ...
This tutorial explains how you can upload files to Google Drive that are submitted through a web form and encoded as multipart/form-data.
Read more >
Uploading Files Using Multer in a Node.js Application
In this article, we will see how to use Multer to handle multipart/form-data using Node.js, Express and MongoDB.
Read more >
Multer: Easily upload files with Node.js and Express
Encoding and uploading forms with Multer ; enctype attribute, which specifies how data should be encoded by the browser before sending it to...
Read more >
Google Cloud Storage with Node.js: File Upload example
Open command prompt, change current directory to the root folder of our project. Install GCS, Express, Multer, CORS modules with the following ...
Read more >
Google drive API upload file with Nodejs + service account
The Google Drive api allows us to create and upload the binary type files directly to Google drive with Node js. The Google...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found