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.

multer saving to var folder; not listening to storage options

See original GitHub issue

I would like it to save in the uploads directory but it keeps saving to /var/ .

Here is the console output showing the path that it chooses followed by my code snippets.

{ host: 'localhost:3000', connection: 'keep-alive', 'content-length': '1140', 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36', 'cache-control': 'no-cache', origin: 'chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop', 'postman-token': '15334565-30f7-aeb8-116b-e697e2ab5ff3', 'content-type': 'multipart/form-data; boundary=----WebKitFormBoundaryLBmxiB0pFtgnhG4u', accept: '*/*', 'accept-encoding': 'gzip, deflate, br', 'accept-language': 'en-US,en;q=0.9' } STARTING UPLOAD { fieldName: 'image', originalFilename: 'compilers.asm', path: '/var/folders/bt/x2tr818x1wxgybgc8z11h4080000gp/T/1615-1cloh49.w3pexez5mi.asm', headers: { 'content-disposition': 'form-data; name="image"; filename="compilers.asm"', 'content-type': 'application/octet-stream' }, ws: WriteStream { _writableState: WritableState { objectMode: false, highWaterMark: 16384, needDrain: false, ending: true, ended: true, finished: true, decodeStrings: true, defaultEncoding: 'utf8', length: 0, writing: false, corked: 0, sync: false, bufferProcessing: false, onwrite: [Function], writecb: null, writelen: 0, bufferedRequest: null, lastBufferedRequest: null, pendingcb: 0, prefinished: true, errorEmitted: false, bufferedRequestCount: 0, corkedRequestsFree: [Object] }, writable: false, domain: null, _events: { error: [Function], close: [Function] }, _eventsCount: 2, _maxListeners: undefined, path: '/var/folders/bt/x2tr818x1wxgybgc8z11h4080000gp/T/1615-1cloh49.w3pexez5mi.asm', fd: null, flags: 'w', mode: 438, start: undefined, autoClose: true, pos: undefined, bytesWritten: 938, closed: true }, size: 938, name: 'compilers.asm', type: 'application/octet-stream' }

`var path = require(‘path’) var multer = require(‘multer’) var storage = multer.diskStorage({ destination: function (req, file, cb) { cb(null, path.join(__dirname + ‘/uploads/’)) }, filename: function (req, file, cb) { cb(null, file.fieldname + ‘-’ + Date.now()) } })

//specify name of file multer should look for //app.post(‘/images’, upload.single(‘image’), images.create)

// var cpUpload = upload.fields([{ name: ‘image’, maxCount: 1 }, { name: ‘resume’, maxCount: 1 }]) // app.post(‘/inputs’, cpUpload, inputs.create)

var upload = multer({storage:storage}).single(‘image’)

exports.create = (req, resp) => { console.log(req.headers) console.log(‘STARTING UPLOAD’) upload(req, resp, err => { if (err) { console.log(‘error=’ + err) } console.log(req.files[‘image’]) resp.json({ success: true, message: ‘image uploaded’ }) })

}`

In Express, I am also using a public folder to serve up the app.

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
HarshithaKPcommented, Oct 14, 2019

@zhughes3 @s00500 I was able to recreate the problem. Made minor changes, now it is working fine.

var directory = path.join(__dirname+'/uploads');
var storage = multer.diskStorage({
        destination: directory,
        filename: function (req, file, cb) {
        cb(null, file.fieldname + '-' + Date.now())
        }
});

var upload = multer({storage:storage}).single('file')

app.post('/',(req, res) =>{
        upload(req, res, (err) => {
                if (err) {
                        console.log('error=');
                }
        else {
                console.log(req.file);
                res.json({
                        success: true,
                        message: 'image uploaded'
                        });
              }
        });

});
1reaction
s00500commented, Nov 5, 2018

Having kind of the same problem on macOS, node 10.10

Read more comments on GitHub >

github_iconTop Results From Across the Web

Multer isn't saving to specified destination - node.js
It seems to be setting the file correctly, but I'm not sure where it gets the destination from, no3 do I understand why...
Read more >
How to set different destinations in nodejs using multer
I'm trying to upload any file using Multer package. It's working fine when I use following code in my server.js file. var express ......
Read more >
File upload with multer in node.js and express
You will notice that the uploads folder is created in the location provided in dest option(in our case in the project directory) but...
Read more >
Upload files to server using Node.js and Multer package
In this article I will tell about how to upload files to server using Node.js and multer package from npm, filter upload files...
Read more >
Uploading Files Using Multer in a Node.js Application
We are going to store the uploaded files in our disk storage inside the files folder that we just created. Let's start by...
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