XMLHttpRequest 2.0与FileReader接口的方法
jsonpd的实现:
var jsonp = function (options) {
var url = options.url,
params = options.params || {},
callbackKey = options.callbackKey || 'callback',
callback = options.callback;
var script = document.createElement('script');
var arr = [];
for (var key in params) {
arr.push(key + '=' + params[key]);
}
params = arr.join('&');
script.src = encodeURI(url + '?' + callbackKey + "=callback&"+params );
document.body.appendChild(script)
window.callback = callback;
}
jsonp({
url:'http://127.0.0.1:3000',
callbackKey: 'callback',
callback:function (data) {
console.log(data)
},
params: {
key: 'excited',
},
})
有:
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属性中。
| 方法名 | 参数 | 描述 |
|---|---|---|
| readAsBinaryString | file | 将文件读取为二进制编码 |
| readAsText | file,[encoding] | 将文件读取为文本 |
| readAsDataURL | file | 将文件读取为DataURL |
| abort | (none) | 终端读取操作 |
2、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 === || _this.nodeType === || !_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){}/;
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[].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[];
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接口的方法的更多相关文章
- 毕业论文中使用的技术—FileReader接口
用来把文件读入内存,并且读取文件中的数据. FileReader接口提供了一个异步API,使用该API可以在浏览器主线程中异步访问文件系统,读取文件中的数据 FileReader接口的方法 方法名 参 ...
- HTML5学习之FileReader接口
http://blog.csdn.net/zk437092645/article/details/8745647 用来把文件读入内存,并且读取文件中的数据.FileReader接口提供了一个异步API ...
- Html5 js FileReader接口
用来把文件读入内存,并且读取文件中的数据.FileReader接口提供了一个异步API,使用该API可以在浏览器主线程中异步访问文件系统,读取文件中的数据.到目前文职,只有FF3.6+和Chrome6 ...
- as3.0 interface接口使用方法
[转]as3.0 interface接口使用方法 AS在2.0的时候就支持接口了 接口能够让你的程序更具扩展性和灵活性,打个例如 比方你定义了一个方法 代码: public function aMet ...
- HTML5 FileReader接口学习笔记
1.FileReader概述 FileReader 对象允许Web应用程序异步读取存储在用户计算机上的文件(或原始数据缓冲区)的内容,使用 File 或 Blob 对象指定要读取的文件或数据. 其中F ...
- java中获取接口(方法)中的参数名字(eclipse设置编译参数)(java8 javac -parameters)
interface接口参数 jdk1.7及以前使用spring功能实现的: 注意: 1.该功能只能获取类的方法的参数名,不能获取接口的方法的参数名. public static void test() ...
- Spring自定义一个拦截器类SomeInterceptor,实现HandlerInterceptor接口及其方法的实例
利用Spring的拦截器可以在处理器Controller方法执行前和后增加逻辑代码,了解拦截器中preHandle.postHandle和afterCompletion方法执行时机. 自定义一个拦截器 ...
- Java6.0中Comparable接口与Comparator接口详解
Java6.0中Comparable接口与Comparator接口详解 说到现在,读者应该对Comparable接口有了大概的了解,但是为什么又要有一个Comparator接口呢?难道Java的开发者 ...
- Java基础学习笔记十二 类、抽象类、接口作为方法参数和返回值以及常用API
不同修饰符使用细节 常用来修饰类.方法.变量的修饰符 public 权限修饰符,公共访问, 类,方法,成员变量 protected 权限修饰符,受保护访问, 方法,成员变量 默认什么也不写 也是一种权 ...
随机推荐
- 无序数组的中位数(set+deque)hdu5249
KPI Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- 树形DP+RMQ+单调队列(Bob’s Race HDU4123)
题意:有n个房子,这些房子被n-1条道路连接,有一些运动员从一个房子为起点尽可能跑最远的距离且不能通过一条道路超过两次,这些运行员不能选择同样的起点,这些运动员跑的最远距离和最近距离的差值不能超过Q, ...
- 分享Centos作为WEB服务器的防火墙规则
# Firewall configuration written by system-config-firewall # Manual customization of this file is no ...
- 插入多行数据和类似 select union 方法
Cite:http://blog.csdn.net/downmoon/article/details/5936706 [ruby] view plaincopyprint? Create table ...
- angular 自定义指令
Template-expanding directive: <div ng-controller="Controller"> <div my-customer&g ...
- visualvm添加远程管理-centos
VisualVM连接远程服务器有两种方式:JMX和jstatd,两种方式都不能完美支持所有功能,例如JMX不支持VisualGC,jstatd不支持CPU监控,实际使用可同时配置上并按需选用. 1.修 ...
- js对象遍历
js对象遍历可以使用比较普遍的方法:如下 var ss={aa:"aa",bb:"bb"}; for(var s in ss){ console.info(&q ...
- 把Nodepad++添加进右键菜单
1.运行注册表编辑器:开始->运行->regedit 2.找到HKEY_CLASSES_ROOT/*/shell 3.右击shell,选择 新建->项 4.项的名字为Edit wit ...
- Ubuntu + CentOS7 搭建tftp Server
基于Ubuntu系统做的tftp服务器,基于CentOS 7都差不多,书写了关键命令,测试过Ubuntu 12.0.4 和CentOS 7环境 1.介绍tftp服务器 TFTP(Trivial ...
- android 应用架构随笔一(架构搭建)
1.拷贝积累utils以及PagerTab类 2.定义BaseApplication类 3.定义BaseActivity类 4.改写MainActivity 5.定义布局文件 6.定义BaseFrag ...