File upload with ng-file-upload throwing error

时间秒杀一切 提交于 2019-12-24 00:45:59

问题


I'm using ng-file-upload for uploading an image file. The code for html for the upload button is

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

and the code for controller is as follows

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);
        });
    }

When I click the upload button the following error is being thrown..

undefined is not a function at Upload.upload

I've inject the Upload service in controller and the sequence of the included js files are as follows

angular.js

ng-file-upload-shim.js

ng-file-upload.js

So, I don't know where the problem is. Thanks in advance....

Edit stacktrace

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

回答1:


It seems like you haven't properly initialized/ injected the dependencies.

In the module:

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

In the controller:

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);
        });
    }
}



回答2:


if you want to go for vanilla js , this piece of code will work for any library or no library and compatible with 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);


来源:https://stackoverflow.com/questions/34194189/file-upload-with-ng-file-upload-throwing-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!