angular文件上传未将文件添加到请求中

angular-file-upload not adding file to the request

本文关键字:文件 请求 添加 angular      更新时间:2023-09-26

我正在使用angular文件上传将图片上传到运行Node和Express 4.0的服务器,但在获取服务器上的文件数据时遇到了问题。

这是相关代码:

<input type="file" ng-file-select="onFileSelect($files)">

角度控制器:

$scope.onFileSelect = function ($files) {  
    var file = $files[0];
    // [..] validation
    $scope.upload = $upload.upload({
        url: 'api/settings/picture',
        method: 'POST',
        file: file,
        headers: {'Content-Type': undefined}
      }).progress(function (evt) {
          $scope.percent = parseInt(100.0 * evt.loaded / evt.total);
      }).success(function (data) {
          console.log(data);
      }).error(function () {
          console.log('error');
      });
};

服务器代码:

// Picture
app.route('/api/settings/picture')
  .post(middleware.auth, users.changePicture);

路线处理程序:

exports.changePicture = function (req, res) {
  var userId = req.user._id;
  console.log(req.files);
};

req.files总是undefined

有什么想法吗?

问题是由Express 4.0中的更改引起的。

我用Multer解决了这个问题。

关于这篇文章的更多信息:https://groups.google.com/forum/#!topic/express js/INjdKyS3--0