简体   繁体   中英

Upload base64 image with angular-file-upload

I creating a form in angular where the user can take a photo from the webcam, where I using ng-webcam and after I take the picture, is save it on a scope as a base64 image. Now I trying to upload this with angular-file-upload . But I don't quite understand how it works. The examples that comes on the project is uploading a image from the pc, but I would like to set the base64 image and send it.

How can I do this?

UPDATED

This is how normally I upload a picture from the computer, I use the function called uploadProfilePicture(); So what I want to do, is put inside the request that I suppose is $scope.uploader my $scope.photo that is my base64 image.

//This is where I take the image from de webcam
$scope.onCaptureComplete = function(src) {
  $scope.photo = src[0];
};

$scope.uploader = new FileUploader({
  url: 'api/user/picture'
});

// Called after the user selected a new picture file
$scope.uploader.onAfterAddingFile = function(fileItem) {
  if ($window.FileReader) {
    var fileReader = new FileReader();
    fileReader.readAsDataURL(fileItem._file);

    fileReader.onload = function(fileReaderEvent) {
      $timeout(function() {
        $scope.imageURL = fileReaderEvent.target.result;
        // console.log($scope.imageURL);
        $scope.localImg = true;
      }, 0);
    };
  }
};

// Called after the user has successfully uploaded a new picture
$scope.uploader.onSuccessItem = function(fileItem, response, status, headers) {
  $scope.localImg = false;

  // Show success message
  $scope.success = true;

  // Populate user object
  $scope.user = Authentication.user = response;

  // Clear upload buttons
  $scope.cancelUpload();
};

// Change user profile picture
$scope.uploadProfilePicture = function() {
  // Clear messages
  $scope.success = $scope.error = null;

  // Start upload
  $scope.uploader.uploadAll();
};

Try this it's working for me.

View:

<img ng-src="data:image/jpeg;base64,{{unImage.image}}" class="images" />

<div class="col-md-9 lato size-twelve grey" editable-file="editImage" e-onchange="angular.element(this).scope().uploadFile(this, angular.element(this).scope().allUnits.images)" e-rows="7" e-cols="40" e-name="image" e-form="rowform" e-multiple>
</div>

Controller function:

$scope.uploadFile = function (input, allUnits) {
            allUnits.images = [];
            if (input.files && input.files[0]) {
                for (var i = 0; i < input.files.length; i++) {
                    allUnits.images.push(input.files[i]);
                }
            }
};

var file=image['images'];
Upload.upload({
                method: 'POST',
                headers: {
                    'Authorization': 'Bearer ' + window.localStorage.getItem('token')
                },
                url: baseUrl + '/test/upload-image',
                data: {id: data.id, file: file}
 });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM