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. 人生苦短之我用Python篇(XML模块)

    XML模块 http://baike.baidu.com/link?url=-mBgvMdEDU7F05Pw7h_hBt7A0ctYiPm5a_WvKVLknydnRXKRIyydcVZWRjd_5H ...

  2. learn go recursive

    package main // 参考文章: // https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/06.6.md im ...

  3. Ubuntu下使用openssl为apache2配置ssl

    安装apache2 sudo apt-get install apache2 安装openssl 一般系统自带,可以通过openssl version查看,如果没有, sudo apt-get ins ...

  4. 关于掌握C#的内存堆栈概念

    很多时候,我们使用C#语言书写业务逻辑时,并不会太多地主动考虑到内存的占用和分配问题,但编的程序多了,就总会遇到一些性能问题.提到"性能"二字,就不得不考虑CPU和内存,而提到内存 ...

  5. python库openpyxl操作excel

    废话不多说,看代码,不懂的留言. from openpyxl import * class ExcelUtil: ''' 读取excel文件内容''' def create_work_book(sel ...

  6. vmware linux nat模式设置静态ip

    网上资料很多,但是都不怎么实用,这里给大家总结一下.nat模式上网.因为nat本身就能上网为什么还要设置ip.这有点自找麻烦.但是在集群这是必须的.要么你搭建伪分布,要么至少具有三台物理机器.为了节省 ...

  7. pat甲级 1155 Heap Paths (30 分)

    In computer science, a heap is a specialized tree-based data structure that satisfies the heap prope ...

  8. Linux下Apache服务器配置

    Linux下Apache服务器配置 相关包: httpd-2.2.3-29.e15.i386.rpm                 //主程序包 httpd-devel-2.2.3-29.e15.i ...

  9. 错误:Authentication with old password no longer supported, use 4.1 style passwords.

    今天重新装了系统,mysql connector 使用了官网的最新版本(6.8.3). 启动项目出现标题中的错误.原因大家谷歌. 解决方法,使用低版本的 connector. 我使用的是 6.5.6 ...

  10. ballerina 学习六 xml && json

    ballerina xml && json 参考使用 代码比较简单,使用起来还是比较方便的 xml 代码说明: import ballerina/io; function main ( ...