convert image to byte code in javascript IE8

。_饼干妹妹 提交于 2019-12-12 03:07:29

问题


How to convert an Image(png) to byte code in Javascript. I've used this code but in IE8 there is no use in this code because there is no support of canvas element in IE8.

function getBase64Image(){     
    p=document.getElementById("fileUpload").value;
    img1.setAttribute('src', p); 
    canvas.width = img1.width; 
    canvas.height = img1.height; 
    var ctx = canvas.getContext("2d"); 
    ctx.drawImage(img1, 0, 0); 
    var dataURL = canvas.toDataURL("image/png");alert("from getbase64 function"+dataURL );    
    return dataURL;
} 

Is any other way to get image byte code in IE8. I need either from Image to base64 byte code in a html page or from any image url base64 byte code.

My image url is like this is there any other way to get image byte code in javascript.


回答1:


Simple answer is unfortunately you can't - out of the box.

As you say, IE8 does not support the canvas element so there is no way to extract the image data as bytes as you would need to go by the canvas and then use toDataURL or getImageData.

There are poly-fills for IE8 that allows you to use the basic functions such as excanvas. This however does not support pixel extraction as with the two above mentioned metods.

There are two work-arounds:

  1. Use server: send image to server and process it there
  2. Use Flash-based canvas "poly-fills" which allow you to do this.

For the latter point there are a few options such as this one:
http://flashcanvas.net/



来源:https://stackoverflow.com/questions/19970525/convert-image-to-byte-code-in-javascript-ie8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!