【问题标题】:Multer not populating req.files while using postmanMulter 在使用邮递员时没有填充 req.files
【发布时间】:2018-06-10 09:44:09
【问题描述】:

我正在开发一个restful api,它将读取excel表格并以json格式返回表格中的数据。但是在从邮递员上传文件(.xls)并将请求发送到api时。 api 崩溃了。

下面是我的代码:

来自 app.js

var app= express();
var upload = multer({

                    fileFilter : function(req, file, callback) {
                        if (['xls', 'xlsx'].indexOf(file.originalname.split('.')[file.originalname.split('.').length-1]) === -1) {
                            return callback(new Error('Wrong extension type'));
                        }
                        callback(null, true);
                    }
                }).single('file');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

var routes = require("./routes/routes.js")(app,upload);

var server = app.listen(3000, function () {
    console.log("Listening on port %s...", server.address().port);
}); 

routes.js:

app.post('/uploadxl', function(req, res) {
  console.log("upload file");
      var exceltojson = require("xls-to-json-lc");
        upload(req,res,function(err){
          console.log("e");
            if(err){
                 res.json({error_code:1,err_desc:err});
                 return;
            }
            /** Multer gives us file info in req.file object */
            if(!req.file){
                res.json({error_code:1,err_desc:"No file passed"});
                return;
            }
            //start convert process
            /** Check the extension of the incoming file and
             *  use the appropriate module
             */
            if(req.file.originalname.split('.')[req.file.originalname.split('.').length-1] === 'xlsx'){
                exceltojson = xlsxtojson;
            } else {
                exceltojson = xlstojson;
            }
          var   node_xj = require("xls-to-json");
  node_xj({
    input: req.file.path,  // input xls
    output: "output.json", // output json
    sheet: "a"  // specific sheetname
  }, function(err, result) {
    if(err) {
      console.log("error");
      console.error(err);
    } else {
      console.log("--------result----");
      res.json(result);
    }
  });


        });
    });

}

我在 Windows 命令提示符下收到此错误:

上传文件 e 你错过了一个输入文件

这显然意味着它正在进入multer的误差函数。

【问题讨论】:

  • 你能试试 x-www-form-encoded 格式吗?
  • 我没有上传文件的选项,已经试过了

标签: node.js postman multer


【解决方案1】:

您的问题似乎类似于this 问题。

如果您使用的是 Postman,您可以尝试删除 Header:“Content-type”:“multipart/form-data”。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2017-11-24
  • 1970-01-01
  • 1970-01-01
  • 2018-02-11
  • 2021-04-04
  • 1970-01-01
  • 2021-08-29
  • 1970-01-01
相关资源
最近更新 更多