suchen

Heim  >  Fragen und Antworten  >  Hauptteil

node.js - express 无法接受到jQuery ajax 过来的formdata数据

ajax部分

  $.ajax({
          url: '/article/create',
          type: 'POST',
          data: formData,//<----要传输的数据
          async: false,
          cache: false,
          timeout:100,
          contentType: false,//<----头信息设置为false
          processData: false,
          success: function(data){
           alert("文件上传成功");
            },
          error: function(xhr){
              alert('文件上传失败:原因是' + xhr.status);
          }
      });//end ajax

express代码

var multer  = require('multer')
var storage =  multer.diskStorage({
    destination: function (req, file, cb){
        cb(null, './public/upload')
    },
    filename: function (req, file, cb){
        cb(null, file.originalname)
    }
});
var uploadsingle = multer({
    storage: storage
});
exports.savePost = function (req,res,next) {
  // console.log(req.files); 打印出来是undefined
   var upload=uploadsingle.single('file');
  
   upload(req, res, function (err) {
    //  console.log(req);
    console.log('ok');
    console.log(err);
     next()
   })
巴扎黑巴扎黑2786 Tage vor384

Antworte allen(1)Ich werde antworten

  • 伊谢尔伦

    伊谢尔伦2017-04-17 16:31:37

    npm install --save multer

    Antwort
    0
  • StornierenAntwort