1,概述

  Web应用应该具备处理广泛用户输入问题的能力,例如在Web富应用中,用户希望上传文件到服务器。File API定义了访问文件的基本操作途径,包括文件、文件列表集、错误处理等,同时,File API还定义了描述文件异步处理进程中的一些元数据。接下来,我们一起看看File的应用。

2,FileList接口

 接口描述:

 interface FileList {
getter File? item(unsigned long index);
readonly attribute unsigned long length;
};

由FileList对象实现,它表示上传文件的集合列表。如下:

<input type="file" multiple="multiple" name="file" id="file" />

var fileList = document.forms['formName']['file'].files;
fileList有如下属性:
1,length:表示文件列表长度,即文件数量
fileList有如下方法:
1,index(indexNum):indexNum是文件在文件列表中的位置,从0开始,和普通数组下标一样,如果不存在,则返回null.

3,Blob接口

  接口描述:

 interface Blob {

       readonly attribute unsigned long long size;
readonly attribute DOMString type; //slice Blob into byte-ranged chunks
Blob slice(optional long long start,
optional long long end,
optional DOMString contentType); };

由Bob对象实现,它是一个原始数据对象。如下:

 // Create a new Blob object

 var a = new Blob();

 // Create a 1024-byte ArrayBuffer
// buffer could also come from reading a File var buffer = new ArrayBuffer(1024); // Create ArrayBufferView objects based on buffer var shorts = new Uint16Array(buffer, 512, 128);
var bytes = new Uint8Array(buffer, shorts.byteOffset + shorts.byteLength); var b = new Blob(["foobarbazetcetc" + "birdiebirdieboo"], {type: "text/plain;charset=UTF-8"}); var c = new Blob([b, shorts]); var a = new Blob([b, c, bytes]); var d = new Blob([buffer, b, c, bytes]);

Bolb有如下属性:

  1,size:数据的大小

  2,type:数据的MIME类型

Bolb有如下方法:

  1,slice:用来读取原始数据中的某块数据,详情见如下例子

 // obtain input element through DOM

     var file = document.getElementById('file').files[0];
if(file)
{
// create an identical copy of file
// the two calls below are equivalent var fileClone = file.slice();
var fileClone2 = file.slice(0, file.size); // slice file into 1/2 chunk starting at middle of file
// Note the use of negative number var fileChunkFromEnd = file.slice(-(Math.round(file.size/2))); // slice file into 1/2 chunk starting at beginning of file var fileChunkFromStart = file.slice(0, Math.round(file.size/2)); // slice file from beginning till 150 bytes before end var fileNoMetadata = file.slice(0, -150, "application/experimental");
}

4,File接口

  接口描述:

  interface File : Blob {
readonly attribute DOMString name;
readonly attribute Date lastModifiedDate;
};

由File对象实现,它继承自Blob对象,指向一个具体的文件。

File有如下属性:

  1,name:文件的名称

2,lastModifiedDate:文件的最后修改时间

5,FileReader接口

  接口描述:

     interface FileReader: EventTarget {

       // async read methods
void readAsArrayBuffer(Blob blob);
void readAsBinaryString(Blob blob);
void readAsText(Blob blob, optional DOMString encoding);
void readAsDataURL(Blob blob); void abort(); // states
const unsigned short EMPTY = 0;
const unsigned short LOADING = 1;
const unsigned short DONE = 2; readonly attribute unsigned short readyState; // File or Blob data
readonly attribute any result; readonly attribute DOMError error; // event handler attributes
attribute [TreatNonCallableAsNull] Function? onloadstart;
attribute [TreatNonCallableAsNull] Function? onprogress;
attribute [TreatNonCallableAsNull] Function? onload;
attribute [TreatNonCallableAsNull] Function? onabort;
attribute [TreatNonCallableAsNull] Function? onerror;
attribute [TreatNonCallableAsNull] Function? onloadend; };

由FileReader对象实现,用来读取文件里面的数据,提供三个常用的读取文件数据的方法,另外读取文件数据也提供了异步方式。

FileReader属性列表:

事件列表 事件说明
onloadstart 文件读取开始时触发
onprogress  当读取进行中时定时触发。事件参数中会含有已读取总数据量。
onabort 当读取被中止时触发。
onerror 当读取出错时触发。
onload 当读取成功完成时触发。
onloadend 当读取完成时,无论成功或者失败都会触发

FileReader方法列表:

方法列表   方法说明
readAsBinaryString() 读取文件内容,读取结果为一个 binary string。文件每一个 byte 会被表示为一个 [0..255] 区间内的整数。函数接受一个 File 对象作为参数。
readAsText() 读取文件内容,读取结果为一串代表文件内容的文本。函数接受一个 File 对象以及文本编码名称作为参数。
readAsDataURL 读取文件内容,读取结果为一个 data: 的 URL。DataURL 由 RFC2397 定义

参考出处:http://www.w3.org/TR/file-upload/

HTML5 File API解读的更多相关文章

  1. HTML5 File API — 让前端操作文件变的可能

    前言 在 HTML5 File API 出现之前,前端对于文件的操作是非常有局限性的,大多需要配合后端实现.出于安全角度考虑,从本地上传文件时,代码不可能获取文件在用户本地的地址,所以纯前端不可能完成 ...

  2. Resumable.js – 基于 HTML5 File API 的文件上传

    Resumable.js 是一个 JavaScript 库,通过 HTML5 文件 API 提供,稳定和可恢复的批量上传功能.在上传大文件的时候通过每个文件分割成小块,每块在上传失败的时候,上传会不断 ...

  3. HTML5 File api 实现断点续传

    目前市场上大多数的网站的断点上传都是需要安装浏览器插件的,本文就针对高级浏览器的环境下,通过HTML5 File api实现断点上传进行说明 一.实现文件多选 HTML5的<input>新 ...

  4. HTML5 file api读取文件的MD5码工具

    1.工具的用途:用HTML5 file api读取文件的MD5码.MD5码在文件的唯一性识别上有很重要的应用,业内常用MD5进行文件识别.文件秒传.文件安全性检查等: 2.适用性:IE.Chrome皆 ...

  5. HTML5 File API 全介绍

    在 HTML5 File API 出现之前,前端对于文件的操作是非常有局限性的,大多需要配合后端实现.出于安全角度考虑,从本地上传文件时,代码不可能获取文件在用户本地的地址,所以纯前端不可能完成一些类 ...

  6. HTML5 File API的应用

    HTML5 File API简介 HTML5File API协议族 Directories and System   文件系统和目录读取 FileWriter   写入文件 FileReader   ...

  7. HTML5 file API加canvas实现图片前端JS压缩并上传

    一.图片上传前端压缩的现实意义 对于大尺寸图片的上传,在前端进行压缩除了省流量外,最大的意义是极大的提高了用户体验. 这种体验包括两方面: 由于上传图片尺寸比较小,因此上传速度会比较快,交互会更加流畅 ...

  8. HTML5 File API

    1.File API 一直以来,不能直接访问用户计算机中的文件都是web应用开发当中的一大障碍.File API的宗旨是为web开发人员提供一种安全的方式,以便在客户端访问用户计算机中的文件,并更好的 ...

  9. 图片上传插件ImgUploadJS:用HTML5 File API 实现截图粘贴上传、拖拽上传

    一 . 背景及效果 当前互联网上传文件最多的就是图片文件了,但是传统web图片的截图上传需要:截图保存->选择路径->保存后再点击上传->选择路径->上传->插入. 图片 ...

随机推荐

  1. grunt实现修改代码实时刷新浏览器

    grunt例子:https://github.com/Aquarius1993/gruntDemo grunt 实时刷新1:           1.安装chrome浏览器插件:liveReload ...

  2. IOS开发 清空数组正确方法

    NSArray以及NSMutableArray 在Objc中的两种数组(不可变数组和可变数组), 在日常开发中,经常会遇到需要清空数组的情况,很多人下意识的会想到nil这个方法,这里是不提倡的.因为如 ...

  3. slam学习

    学习内容: 数学: 线性代数,概率论, 优化理论,离散数学, 李代数, 凸优化: 算法:   概率机器人, 机器人状态估计, 深度学习,非线性优化: 工程: c/c++ , python, ros, ...

  4. 使用Postman对Restful接口进行测试

    趁着项目需要,花了两天时间对postman进行了一下学习,因为看到各大测试群,各个初入测试的孩子们都在问postman,但其实网上也有很多的教程,这里我就再来发一篇. 1. Http协议 要对接口进行 ...

  5. BZOJ2152 聪聪可可 【点分治】

    BZOJ2152 聪聪可可 Description 聪聪和可可是兄弟俩,他们俩经常为了一些琐事打起来,例如家中只剩下最后一根冰棍而两人都想吃.两个人都想玩儿电脑(可是他们家只有一台电脑)--遇到这种问 ...

  6. 剑指offer第三章

    剑指offer第三章 1.数值的整数次方 给定一个double类型的浮点数base和int类型的整数exponent.求base的exponent次方. class Solution { public ...

  7. jasmine 使用

    1. 下载浏览器运行测试包 https://github.com/jasmine/jasmine/releases 2.  解压,运行包含的测试 SpecRunner.html 3. 测试结果     ...

  8. uGUI知识点剖析之RectTransform

    http://www.2fz1.com/post/unity-ugui-recttransform/#jtss-tsina uGUI知识点剖析之RectTransform 一.基本要点 RectTra ...

  9. FastAdmin 源码分析:jQuery 含逗号的选择器

    FastAdmin 源码分析:jQuery 含逗号的选择器 在 FastAdmin 你常常会看到以下 jQuery 选择器的代码. if ($(".datetimepicker", ...

  10. Linux history时间用户ip设置

    Linux history时间用户ip设置        在使用linux服务器的时候发生一些不知道谁操作的问题,google一下说history命令可以查看到历史记录,用过之后发现还是不够详细,再g ...