错误:无法在'Window'上执行'atob':要解码的字符串未正确编码

时间:2016-09-14 12:50:10

标签: html angularjs

这是我的Javascript代码

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

这是我的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>

我收到错误

  

错误:无法在'Window'上执行'atob':要解码的字符串   未正确编码

2 个答案:

答案 0 :(得分:10)

我遇到了问题。它应该有助于另一个用户保存图像并使用javascript(AnguarJs)压缩图像。

我正在使用此链接压缩图片 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
});

答案 1 :(得分:0)

检查完代码后,您似乎拥有可能不受支持的字符。 Check screenshot 如果这不起作用,请确保您尝试上传的文件的名称已编码为您的数据库或设置支持的名称。

以下是没有这些字符的代码:

var byteCharacters = atob($scope.image1.compressed.dataURL.replace(/^data:image\/(png|jpg);base64,/,'')); var byteNumbers = new Array(byteCharacters.length);