2

It simply doesn't save anything to the destination folder i specified.

i tried {storage:storage} instead of {dest: 'storage/'} but it didn't work either. the image data is actually sent to the server as its console logged. and the dest i specified is created by default but remain empty.

const express = require('express');
const app = express();
const multer = require('multer');
    let storage = multer.diskStorage({
        destination: '/public/my-uploads',
          filename: function (req, file, cb) {
            cb(null, file.fieldname + '-' + Date.now())
          }
    });

const upload = multer({dest:'storage/'}).single('file');


app.post('/upload', upload, (req , res) => {
  console.log(req.files) // this does log the uploaded image data.
})


***** EDIT ******

HTML

        <form  onSubmit={this.upload} enctype='multipart/form-data'>
          <input type='file' name='image' />
          <input type='submit' value='upload' />
        </form>

JS

  upload(e){
    e.preventDefault();
    const file = e.target[0].files[0];
    console.log(file)
    const fm = new FormData();

    fm.append('file', file);
    console.log(fm)
    axios.post('/upload', fm);
  }

POSTMAN

enter image description here

ANUBIS
  • 666
  • 1
  • 9
  • 20
  • possibly duplicate of https://stackoverflow.com/questions/23114374/file-uploading-with-express-4-0-req-files-undefined/47826310#47826310 – Parth Raval Jun 24 '18 at 06:00

6 Answers6

1

Try to catch the error my calling the middleware yourself:

var upload = multer().single('avatar')
app.post('/upload', function (req, res) {
  upload(req, res, function (err) {
    if (err) {
      // An error occurred when uploading
      return
    }

    // Everything went fine
  })
})

Also, change the storage to this:

let storage = multer.diskStorage({
    destination: function(req, file, ca) {
        cb(null, '/public/my-uploads');
    }
    filename: function (req, file, cb) {
        cb(null, file.fieldname + '-' + Date.now())
    }
});
Matt Kuhns
  • 1,328
  • 1
  • 13
  • 26
  • I tried before and tried now again. theres NO ERROR printing!! but it doesn't actually save anything to the folder. – ANUBIS Jun 24 '18 at 05:28
1

It's been a while, the issue was that I wasn't using the multer middleware at all so the callback code for handling the image was never executed. I didn't know much about how express worked back then.

ANUBIS
  • 666
  • 1
  • 9
  • 20
  • 1
    hi @Biss can you let me know how did you fix this? i'm having the same issue. How did you use the middleware? – Ilir Jan 01 '22 at 23:54
0

Seems you are not using the storage variable and use a function for the destination key, as written in the documentation, also you need to pass your file in the input field named field otherwise multer can't store the file, create an storage folder on the same level as the code :

const http = require('http')
const port = 3000
const express = require('express');
const app = express();
const multer = require('multer');
const server = http.createServer(app)

let storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, './storage')
    },
    filename: function (req, file, cb) {
        cb(null, file.fieldname + '-' + Date.now())
    }
});

const upload = multer({ storage }).single('file');


app.post('/upload', upload, (req, res) => {
    console.log(req.files) // this does log the uploaded image data.
})

// bind the server on port
server.listen(port, (err) => {
    if (err) {
        return console.log('something bad happened', err)
    }
    console.log(`server is listening on ${port}`)
})

enter image description here

Constantin Guidon
  • 1,892
  • 16
  • 21
0

The name of the "single" in ...single('file') must match the name in the input (containing the file) <input type="file" name='image' /> - and it does not in your example.

Change the multer-part to this ...single('image') - as in the input name='image'

enter image description here

Marrix
  • 315
  • 3
  • 9
0

Try this File Storage For Save image in Local

const fileStorage = multer.diskStorage({
      destination: (req, file, cb) => {
        cb(null, "images");
      },
      filename: (req, file, cb) => {
        cb(
          null,
          new Date().toISOString().replace(/:/g, "-") + "-" + file.originalname
        );
      },
    });
Smit Gajera
  • 1,001
  • 1
  • 8
  • 26
0

You need to create folder /public/my-uploads first then you can save your file. I am currently learn nodejs and face this issue.

  • HI, maybe you solved your problem creating that directory, but there is a completely different answer, marked as accepted – pierpy May 27 '23 at 15:55
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 27 '23 at 15:55