将数据文件转换为blob

Ful*_*rus 11 javascript html5

如何获得一个blob?

HTML:

<input type="file" onchange="previewFile()">
Run Code Online (Sandbox Code Playgroud)

JavaScript的:

function previewFile() {
  var file    = document.querySelector('input[type=file]').files[0];
  var reader  = new FileReader();

  // Get blob?
  console.log(file);
}
Run Code Online (Sandbox Code Playgroud)

raf*_*uto 21

正如评论中指出的那样,file是blob:

file instanceof Blob; // true
Run Code Online (Sandbox Code Playgroud)

您可以使用文件阅读器API获取它的内容https://developer.mozilla.org/en/docs/Web/API/FileReader

阅读更多:https://developer.mozilla.org/en-US/docs/Using_files_from_web_applications

var input = document.querySelector('input[type=file]');
var textarea = document.querySelector('textarea');

function readFile(event) {
  textarea.textContent = event.target.result;
  console.log(event.target.result);
}

function changeFile() {
  var file = input.files[0];
  var reader = new FileReader();
  reader.addEventListener('load', readFile);
  reader.readAsText(file);
}

input.addEventListener('change', changeFile);
Run Code Online (Sandbox Code Playgroud)
<input type="file">
<textarea rows="10" cols="50"></textarea>
Run Code Online (Sandbox Code Playgroud)

  • @rafaelcastrocouto,@Daniel:如果我想通过 websocket 发送 blob,需要从 abv 代码发送什么对象?我应该发送`ws.send(reader)`吗??? (2认同)

Dip*_*ipo 10

文件对象是 Blob 的实例,但 Blob 对象不是 File 的实例

new File([], 'foo.txt').constructor.name === 'File' //true
new File([], 'foo.txt') instanceof File // true
new File([], 'foo.txt') instanceof Blob // true

new Blob([]).constructor.name === 'Blob' //true
new Blob([]) instanceof Blob //true
new Blob([]) instanceof File // false

new File([], 'foo.txt').constructor.name === new Blob([]).constructor.name //false
Run Code Online (Sandbox Code Playgroud)

如果必须将文件对象转换为 Blob 对象,则可以使用文件的数组缓冲区创建新的 Blob 对象。请参阅下面的示例。

let file = new File(['hello', ' ', 'world'], 'hello_world.txt', {type: 'text/plain'});
//or let file = document.querySelector('input[type=file]').files[0];
let reader = new FileReader();
reader.onload = function(e) {
    let blob = new Blob([new Uint8Array(e.target.result)], {type: file.type });
    console.log(blob);
};
reader.readAsArrayBuffer(file);
Run Code Online (Sandbox Code Playgroud)

正如@bgh 所指出的,您还可以使用 File 对象的 arrayBuffer 方法。请参阅下面的示例。

let file = new File(['hello', ' ', 'world'], 'hello_world.txt', {type: 'text/plain'});
//or let file = document.querySelector('input[type=file]').files[0];

file.arrayBuffer().then((arrayBuffer) => {
    let blob = new Blob([new Uint8Array(arrayBuffer)], {type: file.type });
    console.log(blob);
});
Run Code Online (Sandbox Code Playgroud)

如果您的环境支持 async/await,您可以使用如下所示的单行代码

let fileToBlob = async (file) => new Blob([new Uint8Array(await file.arrayBuffer())], {type: file.type });
console.log(await fileToBlob(new File(['hello', ' ', 'world'], 'hello_world.txt', {type: 'text/plain'})));
Run Code Online (Sandbox Code Playgroud)

  • 如今,使用“arrayBuffer()”方法可能是一个更好的主意(https://developer.mozilla.org/en-US/docs/Web/API/Blob/arrayBuffer)。类似于:`let blob = new Blob(await file.arrayBuffer());` (2认同)