有人知道,是否可以从远程URL读取图像到二进制文件。 我知道File API为我们提供了从本地系统读取二进制文件的可能性。 但我无法找到有关从远程URL读取的信息。也许甚至我可以从浏览器缓存中读取此信息,在我将此信息加载到其他地方之后?
答案 0 :(得分:1)
我认为这会解决您的问题,请将您的网址设为img标记为SRC。
function getBase64Image(img) {
// Create an empty canvas element
var canvas = document.createElement("canvas");
canvas.width = img.width;
canvas.height = img.height;
// Copy the image contents to the canvas
var ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0);
// Get the data-URL formatted image
// Firefox supports PNG and JPEG. You could check img.src to
// guess the original format, but be aware the using "image/jpg"
// will re-encode the image.
var dataURL = canvas.toDataURL("image/png");
return dataURL.replace(/^data:image\/(png|jpg);base64,/, "");
}
此功能在Get image data in JavaScript?
上找到像
一样使用它var img = new Image();
img.load = function(){
var data = getBase64Image(this);
};
img.src= "my IMAGE URL";
或者使用您自己的HTML中的img标记