標籤:
有:
DOMString、Document、FormData、Blob、File、ArrayBuffer
看張大神的部落格吧。http://www.zhangxinxu.com/wordpress/2013/10/understand-domstring-document-formdata-blob-file-arraybuffer/
其中的FileReader介面做一些準備:
1、FileReader介面的方法
FileReader介面有4個方法,其中3個用來讀取檔案,另一個用來中斷讀取。無論讀取成功或失敗,方法並不會返回讀取結果,這一結果儲存在result屬性中。
FileReader介面的方法
| 方法名 |
參數 |
描述 |
| readAsBinaryString |
file |
將檔案讀取為二進位編碼 |
| readAsText |
file,[encoding] |
將檔案讀取為文本 |
| readAsDataURL |
file |
將檔案讀取為DataURL |
| abort |
(none) |
終端讀取操作 |
2、FileReader介面事件
FileReader介面包含了一套完整的事件模型,用於捕獲讀取檔案時的狀態。
FileReader介面的事件
| 事件 |
描述 |
| onabort |
中斷 |
| onerror |
出錯 |
| onloadstart |
開始 |
| onprogress |
正在讀取 |
| onload |
成功讀取 |
| onloadend |
讀取完成,無論成功失敗
|
var UpPreviewImg = function (options) { this.upPreview(options)}UpPreviewImg.prototype = { isIE :function () { //是否是IE return !!window.ActiveXObject; }, isIE6 :function () {//是否是IE6 return isIE() && !window.XMLHttpRequest; }, isIE7 :function () {//是否是IE7 return isIE() && !isIE6() && !isIE8() }, isIE8 :function () { return isIE() && !!document.documentMode; }, setCss : function (_this,cssOption) { if(!_this||_this.nodeType ===3 || _this.nodeType === 8 || !_this.style){ return; } for(var cs in cssOption){ _this.style[cs] = cssOption[cs]; } return _this; }, upPreview:function (options) { var _e = options.e, preloadImg = null; _e.onchange = function () { var _v = this.value, _body = document.body; picReg = /(.JPEG|.jpeg|.JPG|.jpg|.GIF|.gif|.BMP|.bmp|.PNG|.png){1}/; if(!picReg.test(_v)){ alert("請選擇正確的圖片格式"); return false; } if(typeof FileReader == ‘undefined‘) { if (this.file) { var _img = document.createElement("img"); _img.setAttribute("src", this.file.files[0].getAsDataURL()); options.preview.appendChild(_img); } else if (this.isIE6()) { var _img = document.createElement("img"); _img.setAttribute("src", _v); options.preview.appendChild(_img); } else{ _v = _v.replace(/[(‘"%]/g,function (str) { return escape(escape(str)); }); this.setCss(options.preview,{ "filter":"progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=‘true‘,sizingMethod=‘scale‘,src=\‘"+_v+"\‘)","display":"block" }); } } else{ var reader = new FileReader(), _file = this.files[0]; reader.onload = (function (file) { return function () { var _img = document.createElement("img"); _img.setAttribute("src",this.result); options.preview.appendChild(_img); }; })(_file); reader.onerror = function () { alert("檔案讀取資料出錯"); } reader.readAsDataURL(_file); } }}}module.exports = upPreviewImg;
*
* e 長傳的圖片
* preview 展示的div
* @param options
*/一個上傳馬上展示圖片的外掛程式
XMLHttpRequest 2.0與FileReader介面的方法