h5base64转图片、txt文件

function dataURLtoBlob(dataUrl,filetype) {
			    var arr = dataUrl.split(','), 
			    mime = arr[0].match(/:(.*?);/)[1],
			    bstr = atob(arr[1]), 
			    n = bstr.length, 
			    u8arr = new Uint8Array(n);
			    while(n--){
			        u8arr[n] = bstr.charCodeAt(n);
			    }
			    return new Blob([u8arr], {type:mime});
			};
			function dataURLtoBlobTxt(dataUrl) {
			   var file = "";
		    	//去掉url的头,并转换为byte
		    	console.log(dataUrl);
		    	var urlData = b64(dataUrl);
				var bytes = window.atob(urlData);
				//处理异常,将ascii码小于0的转换为大于0
				var ab = new ArrayBuffer(bytes.length);
				var ia = new Uint8Array(ab);
				for(var i = 0; i < bytes.length; i++) {
					ia[i] = bytes.charCodeAt(i);
				}
				//替换掉原先的file
				if(file=="" || file==undefined){
					file = new Blob([ab], {
						type: 'text/plain'
					});
				};
			    return file;
			};

你可能感兴趣的:(h5)