【问题标题】:Req.files is always empty in Express app using Multer使用 Multer 的 Express 应用程序中的 Req.files 始终为空
【发布时间】:2018-12-27 02:46:46
【问题描述】:

我使用 multer 作为我的express app 的中间件,用于在服务器上上传文件,但它似乎无法正常工作,我使用邮递员或 curl 发送的每个帖子请求中都有一个空文件数组。

我在发送请求时已经将标头设置为 'Content-Type':'multipart/form-data'

app.js

行:12-43

const app = express();
const fileStorage = multer.diskStorage({
  destination: (req, file, cb) => {
    cb(null, 'images/');
  },
  Skipfilename: (req, file, cb) => {
    cb(null, new Date().toISOString() + '-' + file.originalname);
  }
});

const fileFilter = (req, file, cb) => {
  if (
   file.mimetype === 'audio/mp3' ||
   file.mimetype === 'image/aug' ||
   file.mimetype === 'image/jpg'
   ) {
    cb(null, true);
  }else {
   cb(null, false);
  }
  };

 app.use(bodyParser.urlencoded()); // x-www-form-urlencoded <form>
 app.use(bodyParser.json()); // application/json

 app.use(
  multer({ storage: fileStorage, fileFilter: fileFilter }).any()
 );

 app.use('/images', express.static(path.join(__dirname, 'images')));

routes/admin.js

行:5-12

const router = express.Router();
router.post('/upload',adminController.upload);
module.exports = router;

controllers/admin.js

exports.upload = (req, res, next) => {  
const files= req.files
console.log(req)
res.status(200).json({ files:files});
}

更新 邮递员请求截图

Request headers

Request body

【问题讨论】:

  • 你能把邮递员的请求贴在这里吗?
  • @ManjeetThakur 我刚刚上传了我的邮递员请求

标签: node.js express multer


【解决方案1】:

在我将 mp3 mimetype 更改为正确的音频/mpeg mimetype 后它开始工作。但这不是一个紧凑的解决方案

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-30
    • 2020-06-09
    • 1970-01-01
    • 1970-01-01
    • 2018-12-21
    • 2018-12-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多