【问题标题】:Return the Array of Bytes from FileReader()从 FileReader() 返回字节数组
【发布时间】:2015-07-15 14:37:09
【问题描述】:

我需要一些帮助,从下面的这个函数返回“bytes”变量以用作另一个函数的输入。

function openfile() {
var input = document.getElementById("files").files;
var fileData = new Blob([input[0]]);

var reader = new FileReader();
reader.readAsArrayBuffer(fileData);
reader.onload = function(){
    var arrayBuffer = reader.result
    var bytes = new Uint8Array(arrayBuffer);
    console.log(bytes);
}

我想获得上述函数的返回值,并在另一个函数中使用字节数组作为输入参数。

【问题讨论】:

  • return bytes; 怎么样?要将它用作另一个函数的参数,请像这样使用它:myfunction(openfile(evt));
  • 如果从“console.log(bytes);”更改,我会得到“未定义” “返回字节;”并在“console.log(openfile());”之外调用

标签: javascript filereader


【解决方案1】:

您可以使用 Promise 等待文件阅读器完成加载您的文件。

Promise 对象用于延迟和异步计算。 Promise 表示尚未完成但预计将来会完成的操作。

Here你可以找到更多关于promise的信息。

这是一个关于如何将承诺融入你的情况的示例。

(function (document) {
  var input = document.getElementById("files"),
      output = document.getElementById('output');

  // Eventhandler for file input. 
  function openfile(evt) {
    var files = input.files;
    // Pass the file to the blob, not the input[0].
    fileData = new Blob([files[0]]);
    // Pass getBuffer to promise.
    var promise = new Promise(getBuffer(fileData));
    // Wait for promise to be resolved, or log error.
    promise.then(function(data) {
      // Here you can pass the bytes to another function.
      output.innerHTML = data.toString();
      console.log(data);
    }).catch(function(err) {
      console.log('Error: ',err);
    });
  }

  /* 
    Create a function which will be passed to the promise
    and resolve it when FileReader has finished loading the file.
  */
  function getBuffer(fileData) {
  	return function(resolve) {
        var reader = new FileReader();
        reader.readAsArrayBuffer(fileData);
        reader.onload = function() {
          var arrayBuffer = reader.result
          var bytes = new Uint8Array(arrayBuffer);
          resolve(bytes);
        }
    }
  }
  
    // Eventlistener for file input.
  input.addEventListener('change', openfile, false);
}(document));
<input type="file" id="files" />
<div id="output"></div>

【讨论】:

  • 不是我想要的。但我想我会改变我计划这样做的方式。谢谢。
  • 请注意,IE 直到版本 11 才支持 Promise 对象。
  • 看起来不错,但对我不起作用。行 var bytes = new Uint8Array(arrayBuffer);有一个错误 TS2345:'string | 类型的参数ArrayBuffer' 不能分配给 'ArrayBuffer | 类型的参数共享数组缓冲区 | ArrayLike'.
  • @mli 您好,请问您使用的是 TypeScript 吗?如果是这样,您确定您的类型转换正确吗?
  • @DavidDomain。是的,它是打字稿。所以我不得不这样说: const bytes = new Uint8Array(reader.result as ArrayBuffer);无论如何谢谢,我只是新手,所以我认为javascript和typescript之间没有区别。
【解决方案2】:

如果您将onload 函数传递给该事件,则可以使其工作。

reader.onload = function(e){
    var arrayBuffer = e.target.result;
    var bytes = new Uint8Array(arrayBuffer);
    console.log(bytes);
}

这会将其从 reader.result 更正为 e.target.result;

此外,使用fileData 时存在问题,将其设置为Blob[files[0]] 并将其发送至reader.readAsArrayBuffer。删除 fileData 并改为使用 reader.readAsArrayBuffer(input[0]); 调用它。

【讨论】:

  • e.target 没有结果属性。
  • @KingWilder 如果您使用的是 IE,则可能需要e.srcElement。如果您使用的是 Chrome,它就在那里。但也有一些可能使它无法工作的东西,OP 基本上使用reader.readAsArrayBuffer(new Blob([input[0]]));,而它应该是reader.readAsArrayBuffer(document.getElementById("files").files[0]);,它最初没有在这里得到解决,并将改变e 是什么。我在编辑中添加了它。
猜你喜欢
  • 1970-01-01
  • 2016-06-01
  • 2022-01-12
  • 1970-01-01
  • 1970-01-01
  • 2018-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多