获取文件名后的FileReader异步加载的文件 [英] Get filename after filereader asynchronously loaded a file

查看:380
本文介绍了获取文件名后的FileReader异步加载的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我加载一个目录中的几个文件,以从他们分析一​​些数据。这个伟大的工程,到目前为止,但我想知道至极文件我在看。所以,我需要被加载后,该文件的名称。 任何人可以帮助的吗?

i am loading several files in a directory to parse some data from them. This works great so far, but i would like to know wich file i am looking at. So i need the name of the file after it was loaded. Can anybody help on that?

//获取目录中的所有文件

// gets all files in dir

function updateData(){
  var dirReader = approot.createReader();

  var fail =failCB('Error - Directory for parsing failed to open'); // logs fail...
  dirReader.readEntries(parseData,fail); 
}

//加载每个文件

// loading each file

function parseData(entries){
  var i;
  for (i=0; i<entries.length; i++) {
    var reader = new FileReader();
    reader.onloadend = createListItem;
    reader.readAsText(entries[i]);
  }
}

//在这里,我想知道这个名字!!!!

// HERE I WOULD LIKE TO KNOW THE NAME !!!!

function createListItem(evt){
    // it gives me all the loaded data. But based on wich file it was, i would like to handle it!
  console.log(evt.target.result)
    // lets say something like this
    $('#content').find(   file.name   ).append(evt.target.result);
  }
}

欢呼的任何建议;)

cheers for any suggestions ;)

推荐答案

创建围绕一个封闭的文件捕捉到当前文件。从你能得到的文件名。我有一个例子在这里:<一href="http://www.html5rocks.com/en/tutorials/file/dndfiles/#toc-reading-files">http://www.html5rocks.com/en/tutorials/file/dndfiles/#toc-reading-files

Create a closure around the File to capture the current file. From that you can get the filename. I have an example here: http://www.html5rocks.com/en/tutorials/file/dndfiles/#toc-reading-files

//关闭捕获文件的信息。

// Closure to capture the file information.

function parseData(entries){
  for (var i=0; i<entries.length; i++) {
    reader.onloadend = (function(file) {
      return function(evt) {
        createListItem(evt, file)
      };
    })(entries[i]);
    reader.readAsText(entries[i]);
  }
}

//和被调用函数获得一个额外的参数

// and the called function gets an additional argument

function createListItem(evt, file) {
  console.log(evt.target.result)
  console.log(file.name);
}

这篇关于获取文件名后的FileReader异步加载的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆