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.

Can´t resend file with memoryStorage option

See original GitHub issue

Hi!

I’ve an Express server which expects a file uploaded via form and then sends that file over to another API.

I tested it saving the file to disk and it works perfectly.

const multer = require('multer');
const fs = require('fs');
const request = require('request');

exports.upload = multer({ dest: 'uploads/' }).single('file');

exports.uploadFile = (req, res) => {
      const formData = {
        name: req.body.name,
        file: fs.createReadStream('uploads/' + req.file.filename)
      }

      const options = {
        url: `${path}/nodes`,
        formData
      }

      request.post(options, (err, response, body) => {
        if (err) return res.status(400).send({ msg: 'error', err });
        return res.status(201).send(response.body);
      });
    })
    .catch(err => res.status(400).send({ msg: 'error', err }));
}

Now, as this is just an intermediate step (for authentication purposes), I don´t really need to save the file to disk, I want to keep it in memory and send it to the other API. So I changed my code:

exports.upload = multer({ storage: multer.memoryStorage() }).single('file');

exports.uploadFile = (req, res) => {
      const formData = {
        name: req.body.name,
        file: req.file.buffer
      }

      const options = {
        url: `${path}/nodes`,
        formData
      }

      request.post(options, (err, response, body) => {
        if (err) return res.status(400).send({ msg: 'error', err });
        return res.status(201).send(response.body);
      });
    })
    .catch(err => res.status(400).send({ msg: 'error', err }));
}

But this is not working. I tried many things, encode it to base64 before sending it, create a bufferStream with new stream.PassThrough() but nothing seems to work. Am I doing anything wrong here?

Thanks

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
stringbeanscommented, Apr 25, 2018

@davidanaya did you ever find solution for this? im doing a very similar thing except using FormData to “forward” the upload to a different api. It works perfectly fine if I use LocalStorage but when using MemoryStorage and using the file.buffer it doesnt seem to work

0reactions
jonathangaldinocommented, May 12, 2019

I’m having the same issue. Did you find a solution?

Read more comments on GitHub >

github_iconTop Results From Across the Web

how to upload file saved in memory by multer to another API
I've found the way to upload files with multer through buffer and not storing locally... Here is my code.... var multer = require('multer'); ......
Read more >
Messages Displayed on the Control Panel When Using the ...
Message Cause Solution "Authentication has failed." The machine cannot perform authentication. Contact the... "Now loading WSD... Please wait." WSD scanner function is being prepared. Wait...
Read more >
This version has been deprecated - multer - npm
MemoryStorage. The memory storage engine stores the files in memory as Buffer objects. It doesn't have any options. var storage = multer.
Read more >
Ricoh C6503-7503 User Guide.pdf
See "Basic Procedure for Saving Scan Files on a Memory Storage Device", ... You can store files scanned in copier, facsimile, printer, or...
Read more >
selenium-wire - PyPI
Selenium Wire also has it's own options that can be passed in the ... Note that driver.wait_for_request() doesn't make a request, it just...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

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