文件操作API
几个重要的JS对象
关于FileList对象的API的原型:
interface FileList {
getter File? item(unsigned long index);
readonly attribute unsigned long length;
};
Blob对象 W3C的API原型:
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
);
};
File对象 W3C的标准:
interface File : Blob {
readonly attribute DOMString name;
readonly attribute Date lastModifiedDate;
};
FileReader对象 W3C的标准:
[Constructor]
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对象的三个读取文件数据方法
readAsBinaryString(Blob blob) 传入一个Blob对象,然后读取数据的结果作为二进制字符串的形式放到FileReader的result属性中。
readAsText(Blob blob, optional DOMString encoding) 第一个参数传入Blob对象,然后第二个参数传入编码格式,异步将数据读取成功后放到result属性中,读取的内容是普通的文本字符串的形式。
readAsDataURL(Blob blob) 传入一个Blob对象,读取内容可以做为URL属性,也就是说可以将一个图片的结果指向给一个img的src属性。