我正在使用 Angular 7,我的需求是需要获取图片的
我已经尝试了下面的代码,但是没有得到准确的输出:
宽度
、高度
和 大小(以 kb 或 mb 为单位)
,同时我也在尝试将其转换为 blob
。我已经尝试了下面的代码,但是没有得到准确的输出:
var img_url = "http://snook.ca/files/mootools_83_snookca.png";
var blob = new Blob([img_url]);
let reader = new FileReader;
reader.readAsDataURL(blob); // read file as data url
reader.onload = () => { // when file has loaded
console.log(reader.result)
var img:any = new Image();
img.src = reader.result;
img.onload = () => {
this.uploaded_image_width = img.width; //to get image width
this.uploaded_image_height = img.height; //to get image height
this.uploaded_image_url = reader.result; //to get blob image
console.log(reader.result)
};
}
当我安慰blob数据时,数据出现了错误(console.log(reader.result)
),而且在img.onload
函数内部没有执行。
我参考了这个fiddle来实现这个功能: http://jsfiddle.net/guest271314/9mg5sf7o/
onload
。问题在于你在声明钩子之前设置了源:将该行移动到钩子下面,然后你就可以正常运行了! - user4676340