• <i id='SvA4Y'><tr id='SvA4Y'><dt id='SvA4Y'><q id='SvA4Y'><span id='SvA4Y'><b id='SvA4Y'><form id='SvA4Y'><ins id='SvA4Y'></ins><ul id='SvA4Y'></ul><sub id='SvA4Y'></sub></form><legend id='SvA4Y'></legend><bdo id='SvA4Y'><pre id='SvA4Y'><center id='SvA4Y'></center></pre></bdo></b><th id='SvA4Y'></th></span></q></dt></tr></i><div id='SvA4Y'><tfoot id='SvA4Y'></tfoot><dl id='SvA4Y'><fieldset id='SvA4Y'></fieldset></dl></div>
  • <legend id='SvA4Y'><style id='SvA4Y'><dir id='SvA4Y'><q id='SvA4Y'></q></dir></style></legend>
        <bdo id='SvA4Y'></bdo><ul id='SvA4Y'></ul>
    1. <small id='SvA4Y'></small><noframes id='SvA4Y'>

      <tfoot id='SvA4Y'></tfoot>

        Javascript - 从 base64 图像获取扩展名

        Javascript - get extension from base64 image(Javascript - 从 base64 图像获取扩展名)
          <bdo id='rbM3N'></bdo><ul id='rbM3N'></ul>
        • <tfoot id='rbM3N'></tfoot>

            <small id='rbM3N'></small><noframes id='rbM3N'>

                <tbody id='rbM3N'></tbody>
              <i id='rbM3N'><tr id='rbM3N'><dt id='rbM3N'><q id='rbM3N'><span id='rbM3N'><b id='rbM3N'><form id='rbM3N'><ins id='rbM3N'></ins><ul id='rbM3N'></ul><sub id='rbM3N'></sub></form><legend id='rbM3N'></legend><bdo id='rbM3N'><pre id='rbM3N'><center id='rbM3N'></center></pre></bdo></b><th id='rbM3N'></th></span></q></dt></tr></i><div id='rbM3N'><tfoot id='rbM3N'></tfoot><dl id='rbM3N'><fieldset id='rbM3N'></fieldset></dl></div>
                • <legend id='rbM3N'><style id='rbM3N'><dir id='rbM3N'><q id='rbM3N'></q></dir></style></legend>
                  本文介绍了Javascript - 从 base64 图像获取扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个从服务返回的 base64 编码图像,它看起来像这样:

                  <预> <代码>/9J/4AAQSkZJRgABAQEASABIAAD/4Yp2aHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wLwA8P3hwYWNrZXQgYmVnaW49Iu + 7vyIgaWQ9Ilc1TTBNcENlaGlIenJlU3pOVGN6a2M5ZCI/Pgo8eDp4bXBtZXRhIHhtbG5zOng9ImFkb2JlOm5zOm1ldGEvIiB4OnhtcHRrPSJBZG9iZSBYTVAgQ29yZSA0LjEtYzAzNiA0Ni4yNzcwOTIsIEZyaSBGZWIgMjMgMjAwNyAxNDoxNjoxOCAgICAgICAgIj4KICAgPHJkZjpSREYgeG1 ....等

                  我如何检测/检查图像扩展名?

                  解决方案

                  对于 String(可以从图像中解析出来),您可以这样做:

                  //创建 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(f127&&r<2048){t+=String.fromCharCode(r>>6|192);t+=String.fromCharCode(r&63|128)}else{t+=String.fromCharCode(r>>12|224);t+=String.fromCharCode(r>>6&63|128);t+=String.fromCharCode(r&63|128)}}return t},_utf8_decode:function(e){var t="";var n=0;var r=c1=c2=0;while(n191&&r<224){c2=e.charCodeAt(n+1);t+=String.fromCharCode((r&31)<<6|c2&63);n+=2}else{c2=e.charCodeAt(n+1);c3=e.charCodeAt(n+2);t+=String.fromCharCode((r&15)<<12|(c2&63)<<6|c3&63);n+=3}}返回t}}//定义字符串,也意味着你需要知道文件扩展名var encoded = "从您的服务返回的 Base64 编码图像";//解码字符串var decode = Base64.decode(encoded);console.log(解码);//如果文件扩展名未知变种扩展=未定义;//做这样的事情var lowerCase = decoded.toLowerCase();if (lowerCase.indexOf("png") !== -1) extension = "png"否则 if (lowerCase.indexOf("jpg") !== -1 || lowerCase.indexOf("jpeg") !== -1)扩展名=jpg"否则扩展=tiff";//然后显示图像var img = document.createElement("img");img.src = 解码;//或者,你可以这样做img.src =数据:图像/"+扩展+;base64,"+编码;

                  为了完整起见,这里是 来源,我希望这会有所帮助!

                  I have a base64 encoded image returned from a service and it looks like this:

                  /9j/4AAQSkZJRgABAQEASABIAAD/4Yp2aHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wLwA8P3hwYWNrZXQgYmVnaW49Iu+7vyIgaWQ9Ilc1TTBNcENlaGlIenJlU3pOVGN6a2M5ZCI/Pgo8eDp4bXBtZXRhIHhtbG5zOng9ImFkb2JlOm5zOm1ldGEvIiB4OnhtcHRrPSJBZG9iZSBYTVAgQ29yZSA0LjEtYzAzNiA0Ni4yNzcwOTIsIEZyaSBGZWIgMjMgMjAwNyAxNDoxNjoxOCAgICAgICAgIj4KICAgPHJkZjpSREYgeG1.... etc
                  

                  How can i detect / check the image extension?

                  解决方案

                  For a String (which you can parse out of an image) you can do this:

                  // Create Base64 Object
                  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<e.length){n=e.charCodeAt(f++);r=e.charCodeAt(f++);i=e.charCodeAt(f++);s=n>>2;o=(n&3)<<4|r>>4;u=(r&15)<<2|i>>6;a=i&63;if(isNaN(r)){u=a=64}else if(isNaN(i)){a=64}t=t+this._keyStr.charAt(s)+this._keyStr.charAt(o)+this._keyStr.charAt(u)+this._keyStr.charAt(a)}return t},decode:function(e){var t="";var n,r,i;var s,o,u,a;var f=0;e=e.replace(/[^A-Za-z0-9+/=]/g,"");while(f<e.length){s=this._keyStr.indexOf(e.charAt(f++));o=this._keyStr.indexOf(e.charAt(f++));u=this._keyStr.indexOf(e.charAt(f++));a=this._keyStr.indexOf(e.charAt(f++));n=s<<2|o>>4;r=(o&15)<<4|u>>2;i=(u&3)<<6|a;t=t+String.fromCharCode(n);if(u!=64){t=t+String.fromCharCode(r)}if(a!=64){t=t+String.fromCharCode(i)}}t=Base64._utf8_decode(t);return t},_utf8_encode:function(e){e=e.replace(/
                  /g,"
                  ");var t="";for(var n=0;n<e.length;n++){var r=e.charCodeAt(n);if(r<128){t+=String.fromCharCode(r)}else if(r>127&&r<2048){t+=String.fromCharCode(r>>6|192);t+=String.fromCharCode(r&63|128)}else{t+=String.fromCharCode(r>>12|224);t+=String.fromCharCode(r>>6&63|128);t+=String.fromCharCode(r&63|128)}}return t},_utf8_decode:function(e){var t="";var n=0;var r=c1=c2=0;while(n<e.length){r=e.charCodeAt(n);if(r<128){t+=String.fromCharCode(r);n++}else if(r>191&&r<224){c2=e.charCodeAt(n+1);t+=String.fromCharCode((r&31)<<6|c2&63);n+=2}else{c2=e.charCodeAt(n+1);c3=e.charCodeAt(n+2);t+=String.fromCharCode((r&15)<<12|(c2&63)<<6|c3&63);n+=3}}return t}}
                  
                  // Define the string, also meaning that you need to know the file extension
                  var encoded = "Base64 encoded image returned from your service";
                  
                  // Decode the string
                  var decoded = Base64.decode(encoded);
                  console.log(decoded);
                  
                  // if the file extension is unknown
                  var extension = undefined;
                  // do something like this
                  var lowerCase = decoded.toLowerCase();
                  if (lowerCase.indexOf("png") !== -1) extension = "png"
                  else if (lowerCase.indexOf("jpg") !== -1 || lowerCase.indexOf("jpeg") !== -1)
                      extension = "jpg"
                  else extension = "tiff";
                  
                  // and then to display the image
                  var img = document.createElement("img");
                  img.src = decoded;
                  
                  // alternatively, you can do this
                  img.src = "data:image/" + extension + ";base64," + encoded;
                  

                  For completion's sake here's the source and I hope this helps!

                  这篇关于Javascript - 从 base64 图像获取扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Fetch multiple links inside foreach loop(在 foreach 循环中获取多个链接)
                  Backbone Fetch Request is OPTIONS method(Backbone Fetch Request 是 OPTIONS 方法)
                  Fetch API leaks memory in Chrome(Fetch API 在 Chrome 中泄漏内存)
                  How can I download and save a file using the Fetch API? (Node.js)(如何使用 Fetch API 下载和保存文件?(Node.js))
                  Send blob data to node using fetch, multer, express(使用 fetch、multer、express 将 blob 数据发送到节点)
                  Sending a custom User-Agent string along with my headers (fetch)(发送自定义用户代理字符串以及我的标头(获取))
                  • <bdo id='EB7Dm'></bdo><ul id='EB7Dm'></ul>

                            <tfoot id='EB7Dm'></tfoot><legend id='EB7Dm'><style id='EB7Dm'><dir id='EB7Dm'><q id='EB7Dm'></q></dir></style></legend>

                            <small id='EB7Dm'></small><noframes id='EB7Dm'>

                            <i id='EB7Dm'><tr id='EB7Dm'><dt id='EB7Dm'><q id='EB7Dm'><span id='EB7Dm'><b id='EB7Dm'><form id='EB7Dm'><ins id='EB7Dm'></ins><ul id='EB7Dm'></ul><sub id='EB7Dm'></sub></form><legend id='EB7Dm'></legend><bdo id='EB7Dm'><pre id='EB7Dm'><center id='EB7Dm'></center></pre></bdo></b><th id='EB7Dm'></th></span></q></dt></tr></i><div id='EB7Dm'><tfoot id='EB7Dm'></tfoot><dl id='EB7Dm'><fieldset id='EB7Dm'></fieldset></dl></div>

                              <tbody id='EB7Dm'></tbody>