Html 错误:无法在“窗口”上执行“atob”:要解码的字符串未正确编码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39490904/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 13:48:35  来源:igfitidea点击:

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

htmlangularjs

提问by Ved Prakash

This is my Javascript code

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

This is my HTML

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

错误:无法在“窗口”上执行“atob”:要解码的字符串未正确编码

回答by Ved Prakash

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

我得到了我的问题。其他用户使用javascript(AnguarJs)保存图像并压缩图像应该会有所帮助。

I am flowing this link to compress the imageGithub

我正在使用此链接压缩图像Github

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

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

回答by Ved Prakash

After checking out your code it seems like you have characters which are not probably supported. Check screenshotIf that doesn't work make sure the name of the file you are trying to upload is encoded to what your database or settings support.

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

Here is the code without those characters:

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

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