Skip to content Skip to sidebar Skip to footer

File Upload With Ng-file-upload Throwing Error

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

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

Solution 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 = newXMLHttpRequest();
          xhr.withCredentials = true;
          var formData = newFormData;
          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);

Post a Comment for "File Upload With Ng-file-upload Throwing Error"