【问题标题】:How to attach file to post request nodejs?如何附加文件以发布请求nodejs?
【发布时间】:2013-07-15 15:58:15
【问题描述】:

我正在使用 nodejs 进行文件上传。我正在尝试以下链接。但不知道如何发布这些东西。我需要将 JSON 数据与文件一起发布。我该怎么办?

function myCtrl() {

   //an array of files selected
   $scope.files = [];

   //listen for the file selected event
   $scope.$on("fileSelected", function (event, args) {
      alert("file selected:")
      $scope.$apply(function () {            
         //add the file object to the scope's files collection
         $scope.files.push(args.file);
      });
   });

   //the save method
   $scope.save = function(filename) {
      $http({
         method: 'POST',
         url: "http://localhost:3000/uploadfile",
         headers: { 'Content-Type': false },
         //This method will allow us to change how the data is sent up to the
         //server for which we'll need to encapsulate the model data 
         //in 'FormData'
         transformRequest: function (data) {
            var formData = new FormData();
            formData.append("model", angular.toJson(data.model));
            for (var i = 0; i < data.files; i++) {
               //add each file to the form data and iteratively name them
               formData.append("file" + i, data.files[i]);
            }
            return formData;
         },
         //Create an object that contains the model and files which will 
         //be transformed in the above transformRequest method
         data: {files: $scope.files }
      }).
      success(function (data, status, headers, config) {
         alert("success!:"+JSON.stringify(data));
      }).
      error(function (data, status, headers, config) {
         alert("failed!:"+JSON.stringify(data));
      });
   };
}

这是我的角度指令“文件上传”,我用来识别要上传的选定文件。

  angular.module('myApp', []).directive('file-upload', function () {
    return {
        scope: true,        //create a new scope
        link: function (scope, el, attrs) {
            el.bind('change', function (event) {
                var files = event.target.files;
                //iterate files since 'multiple' may be 
                        //specified on the element
                for (var i = 0;i<files.length;i++) {
                    //emit event upward
                    scope.$emit("fileSelected", { file: files[i] });
                }                                       
            });
        }
    };
});

我正在使用 ng-click 我正在调用 save()。不工作。有什么问题?

【问题讨论】:

    标签: node.js angularjs upload angularjs-directive


    【解决方案1】:

    我在您的代码中没有看到任何 ng-click。 正确的方法是绑定到“更改”事件,这似乎就是您正在做的事情。 您可以使用轻量级的 angular-file-upload 库,它可以通过指令执行您正在寻找的事情。 它还支持带有 flash polyfill 的 IE9,因为 IE9 不支持 FormData。

    <script src="angular.min.js"></script>
    <script src="angular-file-upload.js"></script>
    
    <div ng-controller="MyCtrl">
      <input type="file" ng-file-select="onFileSelect($files)" multiple>
    </div>
    

    JS:

    //inject angular file upload directive.
    angular.module('myApp', ['angularFileUpload']);
    
    var MyCtrl = [ '$scope', '$http', function($scope, $http) {
      $scope.onFileSelect = function($files) {
        //$files: an array of files selected, each file has name, size, and type.
        for (var i = 0; i < $files.length; i++) {
          var $file = $files[i];
          $http.uploadFile({
            url: 'my/upload/url',
            file: $file
          }).then(function(data, status, headers, config) {
            // file is uploaded successfully
            console.log(data);
          }); 
        }
      }
    }];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-08
      • 2021-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-04
      • 1970-01-01
      相关资源
      最近更新 更多