Multipart / form-data upload - Nodejs - expressjs [英] Multipart/form-data upload - Nodejs - expressjs

查看:192
本文介绍了Multipart / form-data upload - Nodejs - expressjs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于express.multipart从Express 4.x库中删除,在expressjs中处理文件上传的最佳方式是什么?

Since express.multipart is removed from the Express 4.x library, what will be the best way to handle file upload in expressjs?

推荐答案

刚刚回覆了有关多部分的类似问题。我建议多方:

Just answered a similar question about multipart. I would recommend multiparty:

您是否已节点多尝试吗?以下是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.

这篇关于Multipart / form-data upload - Nodejs - expressjs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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