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.

Uploading image does not seem to grab file.

See original GitHub issue

I’m working on getting image uploads to work with react and multer. However multer doesn’t seem to get a req.file key, only finds req.body.photo key and holds the value '[object File]' when I console it out. Multer goes ahead and create the folders specified in destination but does not store the image there (I’m guessing because it doesn’t have req.file). If you know what the problem is please reply, and in case you interested in SoF points heres a link: http://stackoverflow.com/questions/37473634/uploading-images-with-fetch-to-express-using-multer

server.js

var storage = multer.diskStorage({
        destination: './public/users',
        filename: function (req, file, cb) {
            switch (file.mimetype) {
                case 'image/jpeg':
                    ext = '.jpeg';
                    break;
                case 'image/png':
                    ext = '.png';
                    break;
            }
            cb(null, file.originalname + ext);
        }
    });

var upload = multer({storage: storage});

app.use(upload.single('photo'));

app.post('/uploadUserImage', function (req, res) {
    console.log(JSON.stringify(req.body.photo)) // form fields
    console.log(req.photo) // form files
    console.log(req.file) // form files
    res.send(req.body.photo);
});

client.js

    function uploadImage (image) {
      var formData = new FormData();
      formData.append('photo', image);
      fetch('http://localhost:8080/uploadUserImage/', {
        method:'POST',
         body: formData
      });
    }

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
LinusUcommented, Nov 23, 2019

Seems like this was a client problem that can be solved by the code linked, happy to reopen if we should change something in Multer

1reaction
gjsjycommented, Jun 30, 2016

Finally, i have got something going with this issue. Processing filelist field like this will send the required payload to server by browser, it was an issue with front end and not multer.

Object.keys(data).forEach(( key ) => {
      if (data[key] instanceof FileList) {
        body.append(key, data[key][0], data[key][0].name);
      } else {
        body.append(key, data[key]);
      }
    });
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Quickly Fix Image Upload Issues in WordPress
Another sign of this issue is that your images may not appear in the media library. You can then change the file permissions...
Read more >
7 Reasons Why Images Are Not Loading on Your Website
1. Incorrect File Paths ... When you add images to a site's HTML or CSS file, you must create a path to the...
Read more >
How to Fix Image Upload Issue in WordPress (Step by Step)
First, connect to your website via FTP and then go to /wp-content/ folder. Inside, you'll find the uploads folder, which is where WordPress ......
Read more >
Your image upload has failed. What do you do? - Digett
Right click > Properties (for Windows) or Right click > Get Info (for Mac) on your image to see your file size in...
Read more >
WordPress Can't Upload Images? Here's How to Fix It
The "WordPress can't upload images" error is common, but with the right techniques, it is possible to fix an image upload issue.
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