【问题标题】:node.js to upload image using multer shows undefinednode.js 使用 multer 上传图像显示未定义
【发布时间】:2016-07-04 17:43:06
【问题描述】:

在这里,我尝试从 2 个不同的文件输入上传文件,我可以将其上传到前端,但在后端它仍然是 undefined。尝试了几件事,但没有奏效。

html:

<input type="file" name="file1" file-model = "file1"/>
<input type="file" name="file2" file-model = "file2"/>
<button  ng-click = "uploadFile()">UPLOAD FILES</button>

指令:

angular.module('myApp').directive('fileModel', ['$parse',
  function($parse) {
    return {
      restrict: 'A',
      link: function(scope, element, attrs) {
        var model = $parse(attrs.fileModel);
        var modelSetter = model.assign;

        element.bind('change', function() {
          scope.$apply(function() {
            modelSetter(scope, element[0].files[0]);
          });
        });
      }
    };
  }
])

服务:

angular.module('myApp').service('fileUpload', ['$http',
  function($http) {
    this.uploadFileToUrl = function(file, uploadUrl) {
      var fd = new FormData();
      fd.append('file1', file);
      fd.append('file2', file);
      $http.post(uploadUrl, fd, {
        transformRequest: angular.identity,
        headers: {
          'Content-Type': undefined
        }
      })
    }
  }
]);

控制器:

angular.module('myApp')
  .controller('ContactCtrl', ['$scope', 'fileUpload', '$http',
      function($scope, fileUpload, $http) {
        $scope.uploadFile = function() {
          var file1 = $scope.file1;
          var file2 = $scope.file2;
          console.log('file is ');
          console.dir(file1);
          console.dir(file2);

          var uploadUrl = '/upload';
          fileUpload.uploadFileToUrl({
            file1, file2
          }, uploadUrl);
        };

服务器:

function upload(req,res){
  console.log(req.files.file1);
}

【问题讨论】:

  • console.dir(file1);console.dir(file2); 打印什么?
  • 是的,它确实可以打印
  • 我的意思是他们打印什么?输出是什么?
  • 这是我的浏览器控制台读取的文件 lastModified : 1446532932000 lastModifiedDate : Tue Nov 03 2015 12:12:12 GMT+0530 (India Standard Time) name : "pic03.jpg" size : 76606 type : "image/jpeg" webkitRelativePath : "" proto : Blob
  • Ankit verma ensieg??

标签: javascript angularjs node.js express multer


【解决方案1】:

在nodejs的服务器端找不到文件的一种情况,即您可以在 /api/v1/uploadfile": [{ method: "POST", action: controllers.advertController.videoUpload, middleware: [multipartMiddleware], views: { json: views.jsonView } }],

global.multipartMiddleware = multipart();

在服务器端代码中,在请求的中间件中使用 multipartMiddleware。

控制器(在此您将正确的参数传递给服务)

var file ={} file.file1=file1; file.file2=file2; fileUpload.uploadFileToUrl(file, uploadUrl);

服务(如果你想一次上传多个文件使用循环)

angular.module('myApp').service('fileUpload', ['$http', function ($http) {
    this.uploadFileToUrl = function(file, uploadUrl){
       var fd = new FormData();
       fd.append('file1', file.file1);
       fd.append('file2', file.file2);
       $http.post(uploadUrl, fd, {
          transformRequest: angular.identity,
          headers: {'Content-Type': undefined}
       })
    }
 }]);

【讨论】:

  • 感谢您的解决方案,它帮助了我。我只需要按照您的指示更改服务代码 :)
猜你喜欢
  • 2019-12-07
  • 2017-12-22
  • 1970-01-01
  • 2021-05-02
  • 2016-04-14
  • 2018-12-19
  • 1970-01-01
  • 2017-12-01
  • 2021-07-08
相关资源
最近更新 更多