快递3.4.8照片上传问题 - 如何解决不使用bodyParser()? [英] Express 3.4.8 Photo uploading issue - how to solve without using bodyParser()?

查看:169
本文介绍了快递3.4.8照片上传问题 - 如何解决不使用bodyParser()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码 https://gist.github.com/ yhagio / 10654836

我是新来的,试用了Node.js in Action - Chapter.9(上传照片) )。
作者使用快速版本3.4.0,但是我使用3.4.8,我遇到这个问题,

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:无法读取未定义的属性'photo'

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('/');
      });
    });
  };
}; 

但是我发现在我的app.js(bodyParser()不再使用,因为3.4.8 )

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

app.js (在我的代码Express 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()
...

但是在作者的代码中有bodyParser )

But in author's code has bodyParser().

app.js (作者使用Express 3.4.0

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

所以,我想知道我是否可以通过使用multer来解决这个问题( http ://expressjs-book.com/forums/topic/replacement-for-bodyparser-connect-multipart/ ):

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.

更新:解决方案我想出了

e working(routes / photos.js)

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('/');
        });
      });
    });
  };
}; 


推荐答案

你是否给了节点多方尝试?以下是README中的示例用法:

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);

作者(Andrew Kelley)建议避免bodyParser,所以你是正确的避免它,但多方似乎解决了类似的问题。

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

这篇关于快递3.4.8照片上传问题 - 如何解决不使用bodyParser()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆