The FileReader object asynchronously reads the content of a file.

FileReader对象异步读取文件的内容。

It exposes those 4 reading methods we can use to start a reading process:

它展示了我们可以用来开始阅读过程的四种阅读方法:

  • readAsText()

    readAsText()

  • readAsDataURL()

    readAsDataURL()

  • readAsArrayBuffer()

    readAsArrayBuffer()

  • readAsBinaryString()

    readAsBinaryString()

and an abort() method to halt any reading operation.

abort()方法来停止任何读取操作。

Reading the file is asynchronous, and the object exposes several events we can hook into to follow the progress of the operation:

读取文件是异步的,并且该对象暴露了一些我们可以挂钩以跟踪操作进度的事件:

  • onload triggered when the reading successfully ends

    读取成功结束时触发onload

  • onloadstart triggered when the reading starts

    读取开始时触发onloadstart

  • onprogress triggered when the reading is on progress

    正在进行阅读时触发onprogress

  • onloadend triggered when the reading ends, successfully or not successfully

    读取成功或不成功时触发onloadend

  • onerror triggered when an error occurs

    错误发生时触发onerror

  • onabort triggered when the reading is aborted

    读取onabort终止时触发onabort

Once a reading operation is completed, the result property of FileReader contains the file content.

读取操作完成后,FileReader的result属性将包含文件内容。

The error property contains the error message, if an error occurred, and readyState contains the state of the operations - 0 if no data is loaded, 1 if data loading is in progress, and 2 if the loading has finished.

error属性包含错误消息(如果发生错误), readyState包含操作的状态-如果未加载任何数据,则为0 ,如果正在进行数据加载,则为1 ,如果加载已完成,则为2

readAsText() (readAsText())

Loads the content of a blob in a text string.

将blob的内容加载到文本字符串中。

In this example we use that text and put it into the #content element’s inner HTML:

在此示例中,我们使用该文本并将其放入#content元素的内部HTML中:

//..file is available as a blobconst reader = new FileReader()reader.onload = event => {const text = reader.resultdocument.getElementById('content').innerHTML = text
}reader.onerror = (e) => {console.error(e)
}reader.readAsText(file)

Here’s an example that reads the content of a text file when it’s uploaded using an input element, and prints its content to the console:

这是一个示例,该示例使用input元素读取文本文件在上载时的内容,并将其内容打印到控制台:

<input type="file" allow="text/*" />

const input = document.querySelector('input')input.addEventListener('change', e => {const reader = new FileReader()reader.onload = event => {const text = reader.resultconsole.log(text)}reader.onerror = (e) => {console.error(e)}reader.readAsText(input.files[0])
})

readAsDataURL() (readAsDataURL())

Loads the content of a blob into a Data URL.

将Blob的内容加载到Data URL中

//..file is available as a blobconst reader = new FileReader()reader.onload = event => {const dataURL = event.target.resultdocument.getElementById('image').src = dataURL
}reader.readAsDataURL(file)

readAsArrayBuffer() (readAsArrayBuffer())

Loads the content of a blob into an ArrayBuffer.

将Blob的内容加载到ArrayBuffer

//..file is available as a blobconst reader = new FileReader()reader.onload = event => {const buffer = reader.resultconst data = new Int32Array(buffer)//...
}reader.onerror = (e) => {console.error(e)
}reader.readAsArrayBuffer(file)

翻译自: https://flaviocopes.com/filereader/

FileReader对象相关推荐

  1. FileReader对象和FormData对象

    FormData对象 一.概述 FormData就是表单数据,我们提交表单所用的数据,H5里新加了FormData对象,可以让我们对表单数据进行操作,甚至自己组装表单数据进行提交,而不是单纯的仅仅是页 ...

  2. JavaScript FormData对象,FileReader对象,files属性

    一.ajax与FormData的使用 最近在使用ajax朝后端提交数据时,如果提交的数据都是普通键值对还好说,直接使用ajax默认的格式向后端提交即可. $('#d1').click(function ...

  3. html5中的FileReader对象

    表单中有图片选项,选中图片文件之后要求可以预览.这个功能很多控件都封装好了,但是它们的底层都是FileReader对象. FileReader对象提供了丰富的功能,包括以二进制.以文本方式读取文件内容 ...

  4. H5 FileReader对象

    前言:FileReader是一种异步文件读取机制,结合input:file可以很方便的读取本地文件. input:file 在介绍FileReader之前,先简单介绍input的file类型. < ...

  5. 使用FileReader对象的readAsDataURL方法来读取图像文件

    使用FileReader对象的readAsDataURL方法来读取图像文件 FileReader对象的readAsDataURL方法可以将读取到的文件编码成Data URL.Data URL是一项特殊 ...

  6. 前端FileReader对象实现图片file文件转base64

    1.file转base64具体代码 // 图片file转base64方法(file文件,回调函数)fileToBase64(file, callback) {// 创建FileReader对象(不兼容 ...

  7. 前端头像上传功能实现之base64图片/头像上传 详细解析2【扩展知识FileReader对象】

    将图片变成base64字符串 base64是一种数据格式 就是一个字符串可以当图片来使用 // base64之将图片在前端变为base64格式 1.先获取图片 2.FileReader对象将图片进行转 ...

  8. h5如何上传文件二进制流_前端H5中JS用FileReader对象读取blob对象二进制数据,文件传输...

    HTML5中的Blob对象只是二进制数据的容器,本身并不能操作二进制,故本篇将对其操作对象FileReader进行介绍. FileReader FileReader主要用于将文件内容读入内存,通过一系 ...

  9. js中的Blod、File、FileList、FileReader对象

    前端在下载文件的时候经常会使用到Blob.File.FileReader对象,那么它们到底是干嘛的,怎么使? 一.我们可以通过Blob对象直接来操作二进制文件.后端返给我们文件的内容我们就可以用Blo ...

最新文章

  1. ajax 页面无刷新,Ajax的页面无刷新实现详解(附代码)
  2. Spring Boot中使用@Async实现异步调用
  3. Fedora/Redhat 在线安装更新软件包,yum 篇 ── 给新手指南 (转载)
  4. 学习笔记:DHCP服务器的配置
  5. 【VMCloud云平台】SCCM(二)部署
  6. powerdesigner导入sql生成pdm没有注释_PDM手写签名实现方法
  7. 动态添加ImageView 设置setPadding不起作用问题
  8. contourlet matlab 源码,contourlet_toolbox matlab 238万源代码下载- www.pudn.com
  9. flash 独立播放器
  10. 图扑软件数字孪生民航飞联网,构建智慧民航新业态
  11. 小程序中点击二维码图片预览、长按转发、保存、识别图中二维码
  12. 计算机网络之域名系统DNS
  13. tensorboard可视化问题projector无法展示
  14. Jenkins插件源使用国内镜像中心的最新方法
  15. Unity4.3.1引擎源码编译过程
  16. 安卓手机能提取当前页面的链接吗_如何获取一个app内的网页地址?
  17. ‘数据分析实战’——战略分析案例(某购物商城分析案例)
  18. STEP 7 Micro/WIN V4.0 SP9 for s7-200
  19. onkeyup 事件
  20. HAProxy 简介及配置文件详解

热门文章

  1. php统计男女数量,我国人口突破14亿!2020中国总人口数量及男女比例公布 头寸管理...
  2. 帕尔默企鹅数据集探索性分析
  3. 如何从字符串中提取相关内容
  4. 详解僵尸进程、进程等待
  5. 重磅! 教育部正式宣布大学 “取消” 985/211
  6. 是什么让 AI 时代真正到来
  7. war包放到webapps下,启动tomcat,tomcat正常,却无法加载项目
  8. LVS负载均衡-TUN模式(模拟不同网段)
  9. [计算机毕设]基于java的超市综合管理信息系统设计与实现(项目报告+源代码+翻译)
  10. matlab 韩明距离_汉明距离(差异位点有用的到)