错误:无法在'Window'上执行'atob':要解码的字符串未正确编码(Error:Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded)

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

Choose Material Photo :

Image size is too large

我收到了错误

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

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

Choose Material Photo :

Image size is too large

I am getting error

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

原文:https://stackoverflow.com/questions/39490904

更新时间:2020-02-18 11:57

最满意答案

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

我正在流动此链接来压缩图像 Github

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

});

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

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

});

2017-12-19

相关问答

atob()和btoa()方法允许作者将内容转换为base64编码。 在这些API中,对于助记符,“b”可以被认为代表“二进制”,“a”表示“ASCII”。 实际上,由于主要的历史原因,这些函数的输入和输出都是Unicode字符串。 来自: http : //www.w3.org/TR/html/webappapis.html#atob The atob() and btoa() methods allow authors to transform content to and from the

...

新的Blob构造函数将它遇到的任何字符串编码为UTF-8( http://dev.w3.org/2006/webapi/FileAPI/#constructorBlob )。 由于您正在处理二进制数据,因此该转换为UTF-8多字节表示形式。 相反,在传递给Blob构造函数之前,需要将数据转换为一个字节数组。 在Chrome中,以下代码适用于我: var binary = atob(base64)

var array = new Uint8Array(binary.length)

for( var

...

我总是使用它在Base64中解码和编码,尝试一下 var Base64={_keyStr:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",encode:function(e){var t="";var n,r,i,s,o,u,a;var f=0;e=Base64._utf8_encode(e);while(f

...

tomcat 8中的默认URIEncoding是UTF-8,除非属性org.apache.catalina.STRICT_SERVLET_COMPLIANCE设置为true ,那么它是ISO-8859-1。 在您的情况下,您可以尝试添加以下内容: URIEncoding="ISO-8859-1"

在您的server.xml文件中的Connector元素。 有关所有详细信息,请参阅tomcat文档 。 Default URIEncoding in tomcat 8 is UTF-8 except

...

我遇到了问题。 它应该有助于另一个用户保存图像并使用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|j

...

var str = "eyJpc3MiOiJodHRwczovL2lkZW50aXR5LXN0YWdpbmcuYXNjZW5kLnh5eiIsImF1ZCI6Imh0dHBzOi8vaWRlbnRpdHktc3RhZ2luZy5hc2NlbmQueHl6L3Jlc291cmNlcyIsImNsaWVudF9pZCI6IjY5OTRBNEE4LTBFNjUtNEZFRC1BODJCLUM2ODRBMEREMTc1OCIsInNjb3BlIjpbIm9wZW5pZCIsInByb2ZpbGUiLCJz

...

我经常试图避免使用window.location.hash因为它在浏览器中并不统一。 因此,而不是做以下 window.location.hash = "some hash value";

我会做 window.location.href = window.location.href.split("#")[0] + "#" + encodeURIComponent("some hash value");

此外,虽然Firefox在地址栏中显示解码的哈希值(即''而不是%20),但如果您尝试复制

...

您可以直接获取窗口对象并将属性和方法附加到其上。 beforeEach(function() {

window.api = function() {};

window.atob = function() {};

)};

Found the solution . just need to mock the window object inside a beforeEach block . beforeEach(function() {

var win

...

正如Vache所写, window对象仅存在于浏览器中。 但是,您可以使用env.js模拟浏览器。 加载此脚本后,您可以访问window对象。 As Vache wrote, the window object only exists in browsers. However, you can simulate a browser using env.js. After loading this script, you get access to the window object.

Logo

一站式 AI 云服务平台

更多推荐