本文介绍了如何从FileReader中获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要FileReader API的帮助.有什么办法可以从FileReader外部获取数据.我有一个带有类"的方法,其中使用FileReader读取图像,并且我想将图像数据放入类局部变量中(如以下代码所示).
I need a little help with FileReader API. Is there any way how to get data from FileReader outside it. I have a "class" with method, where image is read with FileReader and I want to put image data to the class local variable (as shown in following code).
我确实知道FileReader是异步工作的,而我的解决方案是错误的.有什么方法可以使其工作吗?谢谢.
I do know that FileReader works asynchronously and my solution is wrong. Is there any way how to make it work? Thank you.
CanvasState.prototype.addImage = function(inputFile) {
var file = inputFile;
var reader = new FileReader();
reader.onload = this.loadImageData;
reader.readAsDataURL(file);
}
CanvasState.prototype.loadImageData = function(e) {
this.hasImage = true;
this.imageData = e.target.result;
}
推荐答案
尝试一下:
CanvasState.prototype.addImage = function(inputFile) {
var file = inputFile, self = this;
var reader = new FileReader();
reader.onload = function(e) {
self.hasImage = true;
self.imageData = e.target.result;
};
reader.readAsDataURL(file);
}
这篇关于如何从FileReader中获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!