base64字符串中的Javascript图像

base64字符串中的Javascript图像,javascript,image,base64,Javascript,Image,Base64,api给send me base64字符串,表示存在图像。 我有一个函数可以将这个字符串传输到blob存储 function b64toPhoto(b64Data, contentType, sliceSize) { contentType = contentType || ''; sliceSize = sliceSize || 512; //var byteCharacters = atob(b64Data); var byt

api给send me base64字符串,表示存在图像。 我有一个函数可以将这个字符串传输到blob存储

function b64toPhoto(b64Data, contentType, sliceSize) {
        contentType = contentType || '';
        sliceSize = sliceSize || 512;

        //var byteCharacters = atob(b64Data);
        var byteCharacters = atob(b64Data.replace(/^data:image\/(png|jpg);base64,/, '')); 

        var byteArrays = [];

        for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) {
          var slice = byteCharacters.slice(offset, offset + sliceSize);

          var byteNumbers = new Array(slice.length);
          for (var i = 0; i < slice.length; i++) {
            byteNumbers[i] = slice.charCodeAt(i);
          }

          var byteArray = new Uint8Array(byteNumbers);

          byteArrays.push(byteArray);
        }

        var blob = new Blob(byteArrays, { type: contentType });
        return blob;
      }
我如何用javascript解码?我用在线转换器(从base64到image)尝试我的字符串,效果很好


谢谢

您只需使用字符串即可显示图像

var image = new Image();
image.src = '<string received>...';
document.body.appendChild(image);
var image=newimage();
image.src='…';
document.body.appendChild(图像);
您可以尝试以下方法:(您可以看到代码的工作原理)
javascript文件:

function getTextToDecode () {


//get the encoded string 
var valuToDecode = document.getElementById("userCode").value;
//this is just for making the decoded image downloadable
var linkImg = valuToDecode.concat('" alt="Red dot" download="ganixo-file.png"> click here to download your img </a>');
document.getElementById("codedImg").innerHTML=  '<a href="data:image/jpeg;base64,'.concat(linkImg);
if (valuToDecode == "") {
    alert("text area is empty ! try again..");
}else{
//this is the answer for your question
//check if there is any errors
    if(!atob(valuToDecode))
        {
            alert("bad format! try again..")
        }else
        {
            //decode the encoded string
            var decodedString = atob(valuToDecode);
           //make the image downloadable
            document.getElementById("decodeResult").value = decodedString;
        }
    

}
}
函数getTextToDecode(){ //获取编码字符串 var valuetodecode=document.getElementById(“用户代码”).value; //这只是为了使解码图像可下载 var linkImg=valuToDecode.concat(“'alt=“Red dot”download=“ganixo file.png”>单击此处下载您的img”);
document.getElementById(“codedImg”).innerHTML='他收到的字符串上已经有了
data:image/png;base64,
前缀。如果收到的字符串中的base64无效,这将不起作用。他是如何从中获取Blob的?如果@dotnetortona声称在线检查的字符串有效,那么代码将起作用。这显示了图像,他想获取ima将数据转换为变量。
b64Data
一定有问题,否则不会出现该错误。可能它的末尾有一些您没有粘贴到联机转换器中的内容。
function getTextToDecode () {


//get the encoded string 
var valuToDecode = document.getElementById("userCode").value;
//this is just for making the decoded image downloadable
var linkImg = valuToDecode.concat('" alt="Red dot" download="ganixo-file.png"> click here to download your img </a>');
document.getElementById("codedImg").innerHTML=  '<a href="data:image/jpeg;base64,'.concat(linkImg);
if (valuToDecode == "") {
    alert("text area is empty ! try again..");
}else{
//this is the answer for your question
//check if there is any errors
    if(!atob(valuToDecode))
        {
            alert("bad format! try again..")
        }else
        {
            //decode the encoded string
            var decodedString = atob(valuToDecode);
           //make the image downloadable
            document.getElementById("decodeResult").value = decodedString;
        }
    

}
}