Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to pass crop image in form? #174

Open
RenukaB30 opened this issue Jan 17, 2017 · 3 comments
Open

How to pass crop image in form? #174

RenukaB30 opened this issue Jan 17, 2017 · 3 comments

Comments

@RenukaB30
Copy link

I have used your crop code in my project, cropping image is showing fine. But how to upload the cropped image via api?

How to pass crop image in form ?
Please help!

@tkehayov
Copy link

tkehayov commented Jan 21, 2017

You can convert cropped image to BLOB and bind result to FormData and send result via http.
blob convert example:

function dataURItoBlob(dataURI) {
      var byteString = atob(dataURI.split(',')[1]);
      var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0]
      var ab = new ArrayBuffer(byteString.length);
      var ia = new Uint8Array(ab);
      for (var i = 0; i < byteString.length; i++) {
        ia[i] = byteString.charCodeAt(i);
      }

      var blob = new Blob([ab], { type: mimeString });
      return blob;
    }

and sending via http

vm.uploadImage = function() {
      var formData = new FormData();
      var croppedImage = dataURItoBlob(vm.myCroppedImage);

      formData.set("file", croppedImage, fileName);
      $http.post("/image", formData, {
        transformRequest: angular.identity,
        headers: {
          'Content-Type': undefined
        }
      }));
    }

Hope this help. :)

@RenukaB30
Copy link
Author

Yes, Thanks. its working fine. :)

@starsoheil2007
Copy link

Hi @tkehayov

I got this error

Error: Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded.

When run this function (dataURItoBlob) ;

Can you help me ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants