簡體   English   中英

Multipart / form-data upload - Nodejs - expressjs

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

由於express.multipart已從Express 4.x庫中刪除,因此在expressjs中處理文件上傳的最佳方法是什么?

剛回答了關於multipart的類似問題 我會推薦multiparty:

您是否試過節點多方 以下是自述文件中的示例用法:

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,所以你應該避免使用bodyParser,但是多方似乎為我解決了類似的問題。

你可以使用connect-multipartyhttps://github.com/andrewrk/connect-multiparty

它可以用作您要接受上傳的路線中的中間件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM