Failed to execute atob on Window

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

解决方案:去掉 base64 中开头的 data:image/png;base64, 即可。

function make_blob_image() {
	
	// 必须去掉 base64 中开头的 "data:image/png;base64," 部分,否则触发异常:Failed to execute 'atob' on 'Window'。
	var base64 = get_image_base64();
	
	let img = document.createElement('img');
	
	// 解码
	let decodedData = window.atob(get_image_base64());
	
	// 每个元素分解为数组
	let typedArray = decodedData.split('')
	
	// 将每个数组元素返回 0 到 65535 之间的整数,表示给定索引处的 UTF-16 代码单元
	let map1 = typedArray.map(function(c) {
		return c.charCodeAt(0);
	})

	// 数组类型表示一个 8 位无符号整型数组,创建时内容被初始化为 0。
	// 创建完后,可以以对象的方式或使用数组下标索引的方式引用数组中的元素。
	let uint8Array = new window.Uint8Array(map1);
	
	
	// 构造函数返回一个新的 Blob 对象。blob 的内容由参数数组中给出的值的串联组成。
	var blob = new Blob(
		[uint8Array],
		{type: 'image/'+getSelectValue($$('#imgType'))}
	)

	// URL.createObjectURL() 静态方法会创建一个 DOMString,其中包含一个表示参数中给出的对象的 URL。这个 URL 的生命周期和创建它的窗口中的 document 绑定。这个新的 URL 对象表示指定的 File 对象或 Blob 对象。
	img.src = URL.createObjectURL(blob);

	document.body.appendChild(img);
}

你可能感兴趣的:(Javascript,数学建模)