FileReader 对象

FileReader 实例中有四个方法来读取文件数据

1、readAsBinaryString(Blob|File)

2、readAsDataURL(Blob|File) 

3、readAsText(Blob|File)

4、readAsArrayBuffer(Blob|File)

FileReader 实例包含很多事件(onerror, onprogress, onload[result])

<input name="file" id="uploadFile" />

 

var file = document.getElementById("uploadFile");
var bufferSize = 1024;
var pos = 0;

var onload = function(e) {
     console.log("Read", e.target.result);
     var img = document.createElement("img");
     img.src = e.target.result;
     img.width = 300;
     img.height = 300;
     document.body.appendChild(img);
};

var onerror = function() {}

var onprogress = function(e) {}

file.onchange = function() {
     if (file.files) file = file.files[0];
     while (pos < file.size) {
        var reader = new FileReader();
        reader.onload = onload;
        reader.onerror = onerror;
        /*reader.readAsText(file.slice(pos, bufferSize));
        pos += bufferSize; */
       
        reader.readAsDataURL(file);
        pos = file.size + 1;
     }
           
}

 

posted @ 2015-03-04 15:44  风之约  阅读(227)  评论(0编辑  收藏  举报