问题
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:
- Use server: send image to server and process it there
- 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