Express 3.4.8 Photo uploading issue - how to solve

2019-03-31 08:33发布

my code at Gist: https://gist.github.com/yhagio/10654836

I'm new to Express, tried from the example of the book "Node.js in Action - Chapter.9"(Uploading photo). The author uses Express version "3.4.0" but I used "3.4.8" and I ran into this issue,

The Error message when I try to upload images:

500 TypeError: Cannot read property 'photo' of undefined

routes/photos.js

...
exports.submit = function (dir) {
  return function (req, res, next) {
    var img = req.files.photo.image; // ---- This 'photo' part is undefined
    var name = req.body.photo.name || img.name;
    var path = join(dir, img.name);

    fs.rename(img.path, path, function (err) {
      if (err) { return next(err); };

      Photo.create({
          name:name,
          path:req.name
        }, function (err) {
        if (err) { return next(err); };
        res.redirect('/');
      });
    });
  };
}; 

but I found that in my app.js (bodyParser() is no longer used since 3.4.8)

app.js(In my code Express 3.4.8)

...
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());       // Instead of bodyParser()
app.use(express.urlencoded()); // Instead of bodyParser()
...

But in author's code has bodyParser().

app.js(Author uses Express 3.4.0

...
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser()); // This is no longer used in latest version

So, I was wondering if I can fix this issue by using multer (http://expressjs-book.com/forums/topic/replacement-for-bodyparser-connect-multipart/):

app.use(express.json());       
app.use(express.urlencoded());
app.use(multer({ dest: './public/photos' })); // I tried this

This didn't solve. Please help me. Thank you.

UPDATE: Solution I figured out

This code worked(routes/photos.js)

exports.submit = function (dir) {
  return function(req, res, next){
    var form = new multiparty.Form();
    form.parse(req, function(err, fields, files){
      var img = files.image[0];
      var name = fields.name || img.originalFilename;
      var path = join(dir, img.originalFilename);
      fs.rename(img.path, path, function(err){
        if(err){return next(err); };
        Photo.create({
          name: name,
          path: img.originalFilename
        }, function(err){
          if(err){return next(err); };
          res.redirect('/');
        });
      });
    });
  };
}; 

2条回答
一夜七次
2楼-- · 2019-03-31 08:55

The reason it no longer works is because the node-formidable library is no longer included in Express. If you want to continue using formidable, follow these instructions.

The proper way to use body-parser with Express 4.x is:

var express = require('express'),
    bodyParser = require('body-parser'),
    app = express();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

To access the files:

var formidable = require('formidable'),
    form = new formidable.IncomingForm();

exports.submit = function (dir) {
    return function (req, res, next) {
        form.parse(req, function(err, fields, files) {
            // The files object here is what you expected from req.files
        });
    });
});

Note: if you're trying to use multiple, set:

form.multiples = true;
查看更多
该账号已被封号
3楼-- · 2019-03-31 08:57

Have you given node-multiparty a try? Here's example usage from the README:

var multiparty = require('multiparty')
  , http = require('http')
  , util = require('util')

http.createServer(function(req, res) {
  if (req.url === '/upload' && req.method === 'POST') {
    // parse a file upload
    var form = new multiparty.Form();

    form.parse(req, function(err, fields, files) {
      res.writeHead(200, {'content-type': 'text/plain'});
      res.write('received upload:\n\n');
      res.end(util.inspect({fields: fields, files: files}));
    });

    return;
  }

  // show a file upload form
  res.writeHead(200, {'content-type': 'text/html'});
  res.end(
    '<form action="/upload" enctype="multipart/form-data" method="post">'+
    '<input type="text" name="title"><br>'+
    '<input type="file" name="upload" multiple="multiple"><br>'+
    '<input type="submit" value="Upload">'+
    '</form>'
  );
}).listen(8080);

The author (Andrew Kelley) recommends avoiding bodyParser, so you're right to avoid it, but multiparty seems to solve a similar issue for me.

查看更多
登录 后发表回答