简体   繁体   English

HTML5文件API:在FileReader回调中获取File对象

[英]HTML5 File API: get File object within FileReader callback

With the new File API in Javascript you can read files in Javascript to create dataURLs to show clientside pictures clientside. 使用Javascript中的新File API,您可以在Javascript中读取文件以创建dataURL以显示客户端的客户端图片。 I'm wondering if you can reach the File object within the FileReader's onload callback. 我想知道你是否可以在FileReader的onload回调中访问File对象。 I will illustrate this with an example: 我将用一个例子来说明这一点:

var div = document.createElement('div');
div.ondrop = function(e) {
  e.preventDefault();
  e.stopPropagation();
  var files = e.dataTransfer.files;
  for ( var i=0; i<files.length; i++ ) {
    var file = files[i]; // this is the file I want!!
    var filereader = new FileReader();
    filereader.onload = function(e) {
      this; // the FileReader object
      e.target; // the same FileReader object
      this.result; // the dataURL, something like data:image/jpeg;base64,.....
      var img = document.createElement('img');
      img.src = this.result;
      img.title = file.fileName; // This won't be working
      document.appendChild(img);
    }
  }
  return false;
}

What I could do - what I do right now - is wrap the contents of the for loop in a function and execute it to create a new scope and keep a file in that scope like so: 我能做什么 - 我现在做的是 - 将for循环的内容包装在一个函数中并执行它以创建一个新的作用域并将文件保存在该作用域中,如下所示:

for ( var i=0; i<files.length; i++ ) {
    var _file = files[i]; // this is the file I want!!
    (function(file) {
      // do FileReader stuff here
    })(_file);
  }

I was just wondering... Maybe I'm missing something. 我只是想知道......也许我错过了什么。 Is there a way to get the File object from within the onload function of the FileReader? 有没有办法从FileReader的onload函数中获取File对象? Both this and e.target are the FileReader object and not the File. thise.target都是FileReader对象而不是File。 Is there something in this or e that is the File?? thise中有什么东西是文件?? I can't find it :( 我找不到它:(

Thanks a bunch. 谢谢一堆。

PS. PS。 A fiddle: http://jsfiddle.net/rudiedirkx/ZWMRd/1/ 小提琴: http//jsfiddle.net/rudiedirkx/ZWMRd/1/

I already found a way. 我已经找到了办法。 Maybe not better than the scope wrapper, but I think it's neater: 也许并不比示波器包装好,但我觉得它更整洁:

for ( var i=0; i<files.length; i++ ) {
    var file = files[i]; // this is the file I want!!
    var filereader = new FileReader();
    filereader.file = file;
    filereader.onload = function(e) {
        var file = this.file; // there it is!
        // do stuff
    }
}

There is now a much easier (apparently faster) way (sync!) to get a file's data URL: 现在有一种更容易(显然更快)的方式(同步!)来获取文件的数据URL:

img.src = URL.createObjectURL(file);

Demo on http://jsfiddle.net/rudiedirkx/ZWMRd/8/show/ of both methods, and the original problem illustrated (drag multiple images and check the title tooltips). 演示两种方法的http://jsfiddle.net/rudiedirkx/ZWMRd/8/show/ ,并说明原始问题(拖动多个图像并检查标题工具提示)。

I don't think this.file is still supported. 我不认为this.file仍然受支持。 When I try to run the answer code, this.file is undefined whereas if I run the code from the question I get the expected results. 当我尝试运行答案代码时,this.file是未定义的,而如果我运行问题中的代码,我会得到预期的结果。 I think you have to use the closure (at least this is how they do it on html5rocks ( Example )). 我认为你必须使用闭包(至少这是他们在html5rocks上做的方式( 例子 ))。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM