使用 ng-file-upload 抛出错误的文件上传

File upload with ng-file-upload throwing error

我正在使用 ng-file-upload 上传图像文件。上传按钮 html 的代码是

<button ng-disabled="croppedDataUrl===''" class="button button-3d button-rounded" ng-click="vm.upload(croppedDataUrl)">Upload</button>

控制器代码如下

vm.upload = function(dataUrl) {
        Upload.upload({
            url: AppConstants.api.images,
            data: {
                uploadedPicture: dataUrl,
                uploadedFrom: 'recipe'
            },
        }).then(function(response) {
            $timeout(function() {
                $scope.result = response.data;
            });
        }, function(response) {
            if (response.status > 0) $scope.errorMsg = response.status + ': ' + response.data;
        }, function(evt) {
            $scope.progress = parseInt(100.0 * evt.loaded / evt.total);
        });
    }

当我点击上传按钮时,抛出以下错误..

undefined is not a function at Upload.upload

我在控制器中注入了上传服务 以及包含的js文件顺序如下

angular.js

ng-file-upload-shim.js

ng-file-upload.js

所以,我不知道问题出在哪里。提前致谢....

编辑堆栈跟踪

TypeError: undefined is not a function
at vm.upload (http://localhost:8888/yn-site/app/dist/js/app.min.js:5000:24)
at fn (eval at <anonymous> (http://localhost:8888/yn-site/app/dist/js/vendor.min.js:13365:15), <anonymous>:4:379)
at callback (http://localhost:8888/yn-site/app/dist/js/vendor.min.js:23613:17)
at Scope.$eval (http://localhost:8888/yn-site/app/dist/js/vendor.min.js:16052:28)
at Scope.$apply (http://localhost:8888/yn-site/app/dist/js/vendor.min.js:16152:25)
at HTMLButtonElement.<anonymous> (http://localhost:8888/yn-site/app/dist/js/vendor.min.js:23618:23)
at HTMLButtonElement.m.event.dispatch (http://localhost:8888/yn-site/js/jquery.js:2:40587)
at HTMLButtonElement.r.handle (http://localhost:8888/yn-site/js/jquery.js:2:37297) vendor.min.js:12520(anonymous function) vendor.min.js:12520(anonymous function) vendor.min.js:9292Scope.$apply vendor.min.js:16157(anonymous function) vendor.min.js:23618m.event.dispatch jquery.js:2r.handle

您似乎没有正确初始化/注入依赖项。

在模块中:

var app = angular.module('your_module', ['ngFileUpload']);

在控制器中:

app.controller('MyCtrl', ['$scope', 'Upload', function ($scope, Upload) {
$scope.upload = function(dataUrl) {
        Upload.upload({
            url: AppConstants.api.images,
            data: {
                uploadedPicture: dataUrl,
                uploadedFrom: 'recipe'
            },
        }).then(function(response) {
            $timeout(function() {
                $scope.result = response.data;
            });
        }, function(response) {
            if (response.status > 0) $scope.errorMsg = response.status + ': ' + response.data;
        }, function(evt) {
            $scope.progress = parseInt(100.0 * evt.loaded / evt.total);
        });
    }
}

如果您想使用 vanilla js,这段代码适用于任何库或没有库,并且与 ng-file-upload 兼容 -

          var xhr = new XMLHttpRequest();
          xhr.withCredentials = true;
          var formData = new FormData;
          formData.append('YOUR_FILE_OBJECT_NAME', file);
          xhr.addEventListener("readystatechange", function() {
            if (this.readyState === 4) {
              console.log(this.responseText);
            }
          });

          xhr.open("POST", "YOUR_URL_HERE");
          xhr.send(formData);