使用dropzone上传nodejs,express和angular的文件

时间:2014-12-05 18:29:50

标签: javascript angularjs node.js express

我无法通过节点获取文件识别服务器端。

特别是在访问请求数据时,(req.files或req.body)

如果有人有任何建议,以下是一些片段。

HTML

<form action="/upload" class="dropzone" drop-zone id="file-dropzone"></form>

角度控制器

'use strict';

angular.module('app')
  .controller('SampleCtrl', function ($scope, $http) {


  })

  .directive('dropZone', function() {
    return function(scope, element, attrs) {
     $(element).dropzone({ 
        url: "/upload",
        maxFilesize: 100,
        maxThumbnailFilesize: 5
    });
  }
});

节点路由器

'use strict';

var express = require('express');
var controller = require('./import.controller');

var router = express.Router();

router.post('/', controller.import);

module.exports = router;

节点控制器

'use strict';

var express = require('express'),
    _ = require('lodash'),
    fs = require('fs'),
    path = require('path'),
    multer = require('multer'),
    bodyParser = require('body-parser'),
    app = express();

app.use(bodyParser.urlencoded({extended: true}));

exports.import = function(req, res) {
    console.log(req.files); // responds with 'undefined'
    console.log(req.body); // responds with {}
};

提前致谢

1 个答案:

答案 0 :(得分:-1)

您必须实际使用文件上传中间件来获取快递以解压该请求的那一部分。来自multer文档:

var express = require('express')
var multer  = require('multer')

var app = express()
app.use(multer({ dest: './uploads/'}));

console.log(req.body)
console.log(req.files)