使用nervgh / angular-file-upload上传角度文件

时间:2017-01-04 17:07:54

标签: javascript angularjs form-data angular-file-upload

我正在通过角色实施https://github.com/nervgh/angular-file-upload插件进行文件上传,我的设置是这样的:

var vm = this,
    apiUrl = appConfig.getItem('BASE_API_URL'), // base url;

vm.uploader = $scope.uploader = new FileUploader({
    url: apiUrl + '/invoices/upload',
    headers: {
      "Content-Type": undefined
    }
});

// this function is triggered by a button outside
vm.uploadAll = function () { 
  vm.uploader.uploadAll();
};

在html上我有

    <input id="uploadFileButton"
       type="file"
       nv-file-select
       uploader="FUCtrl.uploader"
       multiple
       style="display: none;"/> 
    // the display none is due to that this input is click triggered
    // by an outside button

事情是,在客户端开始发布请求,我看到了这一点 enter image description here

图像上传(理论上),但Content-Type未定义,缺少enctype,另一方面在服务器端我有这个

var express = require('express'),
    multer  = require('multer'),
    cors = require('cors');

var app = express();

app.use(cors());
app.get('*', function(){});

app.use(multer({dest:'./uploads/'}).single('photo'));

app.post('/upload', function(req, res){

  console.log('hit');

  console.log(req.body); // form fields
  console.log(req.files); // form files
  res.status(204).end();

});

app.listen(3000);

但是当我收到我在控制台上看到的帖子时     的console.log(req.body); // {}     的console.log(req.files); //未定义

我无法从pdf的上传中获取任何数据

我错过了什么?

2 个答案:

答案 0 :(得分:0)

嘿,我不知道这个插件。但我正在使用这个https://github.com/danialfarid/ng-file-upload插件,我觉得非常有帮助。这是上传文件的最简单方法。

Upload with form submit and validations:

http://jsfiddle.net/danialfarid/maqbzv15/1118/

Upload multiple files one by one on file select:    

http://jsfiddle.net/danialfarid/2vq88rfs/136/

Upload multiple files in one request on file select (html5 only): 

http://jsfiddle.net/danialfarid/huhjo9jm/5/

Upload single file on file select: 

http://jsfiddle.net/danialfarid/0mz6ff9o/135/

Drop and upload with $watch: 

http://jsfiddle.net/danialfarid/s8kc7wg0/400/

Image Crop and Upload 

http://jsfiddle.net/danialfarid/xxo3sk41/590/

答案 1 :(得分:0)

最后,我在HTML

上发现了解决方案
nv-file-select

应该是

nv-file-select=""

并删除

headers: {
  "Content-Type": undefined
}

来自上传器配置,显然那些东西没有设置内容类型和他的边界,所以我把它们拿出来并且让它工作