将数据文件转换为Blob [英] Convert data file to blob

查看:991
本文介绍了将数据文件转换为Blob的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获得斑点?

HTML:

<input type="file" onchange="previewFile()">

JavaScript:

JavaScript:

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

  // Get blob?
  console.log(file);
}

推荐答案

如注释中所述,fileblob:

file instanceof Blob; // true

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

And you can get its content with the file reader API https://developer.mozilla.org/en/docs/Web/API/FileReader

了解更多: https://developer.mozilla.org/zh-CN/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);

<input type="file">
<textarea rows="10" cols="50"></textarea>

这篇关于将数据文件转换为Blob的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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