使用multer(nodeJs)上传文件不起作用

时间:2016-03-13 16:19:32

标签: javascript node.js express pug multer

我正在尝试使用包含带输入的表单的模式上传多个文件。

我的模态(玉模板格式):

#modalAddFile.modal
    form#uploadForm(enctype='multipart/form-data', action='/#{category}/addFiles', method='post')
      .modal-content
        .file-field.input-field
          .btn
            span Ajouter
            input(type='file', name='songs', multiple)
          .file-path-wrapper
            input.file-path.validate(type='text')

      .modal-footer
        a#cancel.modal-action.modal-close.waves-effect.waves-red.btn-flat(href='#!') Annuler
        button#addFile.modal-action.modal-close.waves-effect.waves-green.btn-flat(type='submit', name='action', href='#!') Valider

routes.js:

var express = require('express');
var router = express.Router();
//Upload file
var multer  = require('multer');
var upload = multer({ dest: '../public/test/' });

//Add files
router.post('/:category/addFiles', upload.array('songs'), function (req, res, next) {
  res.redirect('/');
});

module.exports = router;

我没有任何明显的错误(200次成功),我不知道出了什么问题。

1 个答案:

答案 0 :(得分:1)

尝试使用此代码:

var express = require('express');
var router = express.Router();
var multer  = require('multer');
var storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, '../public/test/')
    },
    filename: function (req, file, cb) {
        cb(null, file.fieldname)
    }
});
var upload = multer({ storage: storage });

//Add files
router.post('/:category/addFiles', upload.any(), function (req, res, next) {
  res.redirect('/');
});

module.exports = router;