Ved Prakash Ved Prakash - 1 year ago 237
AngularJS Question

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



This is my Javascript code

function upload(){
var byteCharacters = atob($scope.image1.compressed.dataURL.replace(/^data:image\(png|jpg);base64,/,''));
var byteNumbers = new Array(byteCharacters.length);
for (var i = 0; i < byteCharacters.length; i++) {
byteNumbers[i] = byteCharacters.charCodeAt(i);
}
var byteArray = new Uint8Array(byteNumbers);
var blob = new Blob([ byteArray ], {
type : undefined
});


This is my HTML

<div class="form-group text-16px" style="margin-top: 20px !important;">
<label>Choose Material Photo : </label>
<div>
<input id="materialImage" type="file" accept="image/*" image="image1" resize-max-height="800" resize-max-width="800" resize-quality="0.7" resize-type="image/jpg" file-model="file" name="materialImage" onChange="checkFile()" ng-image-compress/>
<div id="choose-image-compresser">
<div image="image1" result-image="myCompressedImage"></div>
</div>
<img ng-src="{{image1.compressed.dataURL}}" />
<span id="image-size-error" style="color:red;" hidden=""><small>Image size is too large</small></span>
</div>
</div>


I am getting error


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

Answer Source

I got my problem. It should be helpful for another user for save the image and compress the image using javascript(AnguarJs).

I am flowing this link to compress the image Github

https://github.com/oukan/angular-image-compress

var imageData = $scope.image1.compressed.dataURL.toString();
                                var byteCharacters = atob(imageData.replace(/^data:image\/(png|jpeg|jpg);base64,/, ''));
                                var byteNumbers = new Array(byteCharacters.length);
                                for (var i = 0; i < byteCharacters.length; i++) {
                                    byteNumbers[i] = byteCharacters
                                            .charCodeAt(i);
                                }
                                var byteArray = new Uint8Array(byteNumbers);
                                var blob = new Blob([ byteArray ], {
                                    type : undefined
                                });
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download