angular-file-upload with ngImgCrop
Asked Answered
E

2

6

I'm using (ngImgCrop) to crop an image and then upload the cropped image to server using (angular-file-upload).

I can get the $dataURI from the "on-change" option in ngImgCrop. But I need a File instace to call $upload. How can I get the File instance of the cropped image in order to upload :

        $scope.upload = $upload.upload({
            url: '/api/fileupload',
            file: [**file cropped here**]
        }).progress(function (evt) {
            //
        }).success(function (data, status, headers, config) {
            //
        });
Exemplar answered 22/9, 2014 at 19:32 Comment(5)
How can I create a File object from a data URI ?Exemplar
Hi buddy, i'm facing the same problem. As i explain here #26360903 i get the cropped image uploaded, but i have some troubles to get a usable image in the server side. The image doesn't seem to open properly. Did you solve your question with angular-file-upload? I'm using it too for other purposes, but if it works i could use it for uploading a cropped image as well. Thanks anyway!Aggi
No I did the crop in server side. using python Pillow.Exemplar
hey guys I made it, check my update!Mathildamathilde
can you show me how you use the $files from onFileSelect of angular-file-upload as an image attribute for img-cropAllotrope
N
8

I guess you'll find a proper answer in this method. I found it in Github, in the angular-file-upload issues page (https://github.com/nervgh/angular-file-upload/issues/208):

/**
   * Converts data uri to Blob. Necessary for uploading.
   * @see
   *   https://mcmap.net/q/42066/-convert-data-uri-to-file-then-append-to-formdata
   * @param  {String} dataURI
   * @return {Blob}
   */
  var dataURItoBlob = function(dataURI) {
    var binary = atob(dataURI.split(',')[1]);
    var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
    var array = [];
    for(var i = 0; i < binary.length; i++) {
      array.push(binary.charCodeAt(i));
    }
    return new Blob([new Uint8Array(array)], {type: mimeString});
  };

You should be able to get a file instance doing something like this:

var blob = dataURItoBlob($scope.croppedImage);

I don't know if it works in the good way, but it seems.

Nashua answered 15/10, 2014 at 23:30 Comment(0)
M
3

try something like:

 var uploader = $scope.uploader = new FileUploader({
        url: '/saveImagePath',
        autoUpload: false
    });

angular.element(document.querySelector('#fileInput')).on('change',handleFileSelect);

var handleFileSelect=function(evt) {
      var file=evt.currentTarget.files[0];
      var reader = new FileReader();
      reader.onload = function (evt) {
        $scope.$apply(function($scope){
          $scope.myImage=evt.target.result;
        });
      };
      reader.readAsDataURL(file);
    };

the uploader doesn't support base64 images so you'll need to convert the cropped image from base64 to blob

function base64ToBlob(base64Data, contentType) {
        contentType = contentType || '';
        var sliceSize = 1024;
        var byteCharacters = atob(base64Data);
        var bytesLength = byteCharacters.length;
        var slicesCount = Math.ceil(bytesLength / sliceSize);
        var byteArrays = new Array(slicesCount);

        for (var sliceIndex = 0; sliceIndex < slicesCount; ++sliceIndex) {
            var begin = sliceIndex * sliceSize;
            var end = Math.min(begin + sliceSize, bytesLength);

            var bytes = new Array(end - begin);
            for (var offset = begin, i = 0 ; offset < end; ++i, ++offset) {
                bytes[i] = byteCharacters[offset].charCodeAt(0);
            }
            byteArrays[sliceIndex] = new Uint8Array(bytes);
        }
        return new Blob(byteArrays, { type: contentType });
    }

you have to manually attach the files to the queue like this:

$scope.submit = function () {
         var file = base64ToBlob($scope.currentPortfolio.croppedImage.replace('data:image/png;base64,',''), 'image/jpeg');
        uploader.addToQueue(file);
        uploader.uploadAll();

    };

in the server side, you got two types of files one posted as HTML file and another un base64 which is the cropped image.

Mathildamathilde answered 26/9, 2014 at 20:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.