简体   繁体   English

html5 fileReader - 如何只读取文件的前N个字符?

[英]html5 fileReader — how to only read the first N characters of a file?

Currently I use a pattern like the following to read the first 3 characters of a series of files: 目前我使用如下的模式来读取一系列文件的前3个字符:

var files = e.dataTransfer.files;
for (var i = 0, f; f = files[i]; i++) {
  var fr = new FileReader();
  fr.onload = function(e) { 
    var first_three_chars = e.target.result.substr(0,3);
  }
  fr.readAsText(f);
}

The trouble is that I'm only interested in the first 3 characters of the file, whereas this method reads the entire file, wasting lots of memory and time. 麻烦的是我只对文件的前3个字符感兴趣,而这个方法读取整个文件,浪费了大量的内存和时间。 How can I quickly iterate over the files, simply taking quick peeks at the first characters? 如何快速迭代文件,只需快速查看第一个字符?

Edit: slice() was the answer, thanks sshen. 编辑:slice()是答案,谢谢sshen。 Here's how I did it: 我是这样做的:

var files = e.dataTransfer.files;
for (var i = 0, f; f = files[i]; i++) {
  var fr = new FileReader();
   fr.onloadend = function(e) {
    if (e.target.readyState == FileReader.DONE) {
      var first_three_chars = e.target.result;
    }
  };
  var blob = f.slice(0, 3);
  fr.readAsText(blob);
}

You can use the .slice method. 您可以使用.slice方法。 You can read more here 你可以在这里阅读更多

var reader = new FileReader();

reader.onloadend = function(evt) 
{
    if (evt.target.readyState == FileReader.DONE)  // DONE == 2
    {
        alert(evt.target.result);
    }
};

var blob = file.slice(start, stop + 1);
reader.readAsBinaryString(blob);

Not enough rep to comment, so putting some warnings about @Stu Blair solution here: With the Blob.slice method you are taking the bytes from the Blob, not the characters . 没有足够的Blob.slice评论,所以在这里提出一些关于@Stu Blair解决方案的警告:使用Blob.slice方法,您从Blob获取字节 ,而不是字符

For example, this won't work: 例如,这不起作用:

const blob = new Blob(['😀'], {type: 'text/plain'});
const fr = new FileReader();
fr.readAsText(blob); // Fine, fr.result will be '😀'
fr.readAsText(blob.slice(0, 2)); // Not good, fr.result will be '��'

You will have to use FileReader.readAsArrayBuffer to get the bytes. 您将不得不使用FileReader.readAsArrayBuffer来获取字节。 If your encoding is something like utf-8 you will have to read from the beginning. 如果您的编码类似于utf-8,则必须从头开始阅读。

Either way you still have to go through the list of files, the contents of the FileList interface. 无论哪种方式,您仍然必须通过文件列表,FileList接口的内容。 The reason you're reading in the entire file is when you attach onload to every file and call readAsText() If you don't want to read in the entire file, just register an event handler that gets called back with the filelist before the files are loaded and goes through it. 您在整个文件中读取的原因是当您将onload附加到每个文件并调用readAsText()时如果您不想读取整个文件,只需注册一个事件处理程序,该事件处理程序在返回之前使用文件列表进行回调。文件已加载并通过它。 Something like this , where you attach to a form submission or something that expects to get the file list as part of its event object, without reading each one first. 这样的东西,你附加到表单提交或希望将文件列表作为其事件对象的一部分的东西,而不首先读取每个。

<input type="file" id="files" name="files[]" multiple />
<output id="list"></output>

<script>
  function handleFileSelect(evt) {
    var files = evt.target.files; // FileList object

    // files is a FileList of File objects. List some properties.
    var output = [];
    for (var i = 0, f; f = files[i]; i++) {
      var fileName = f.name.substr(0,3);
      output.push('<strong>', fileName, '</strong>');
    }
    document.getElementById('list').innerHTML = '<ul>' + output.join('') + '</ul>';
  }

  document.getElementById('files').addEventListener('change', handleFileSelect, false);
</script>

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

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