通过XMLHttpRequest加载外部图片文件或数据
来源:互联网 发布:资金盘系统源码 编辑:程序博客网 时间:2024/06/18 15:18
Chrome 团队已经封装好了一个库供开发者使用:Chrome Packaged Apps Resource Loader
var remoteImage, container = document.querySelector('.imageContainer'), toLoad = { 'images': [ 'http://myserver.com/image1.png', 'http://myserver.com/image2.png' ] }; // list of image URLstoLoad.images.forEach(function(imageToLoad) { remoteImage = new RAL.RemoteImage(imageToLoad); container.appendChild(remoteImage.element); RAL.Queue.add(remoteImage);});RAL.Queue.setMaxConnections(4);RAL.Queue.start();
如果只想简单的使用,没有这么多需求而去载入一个库,可以简单的封装一个函数来处理:
function loadImage(uri,callback){if(typeof callback!='function'){callback=function(uri){console.log(uri);}}var xhr = new XMLHttpRequest();xhr.responseType = 'blob';xhr.onload = function() {callback(window.URL.createObjectURL(xhr.response));}xhr.open('GET', uri, true);xhr.send();}//使用方法var imgUrl='http:xxx.jpg';loadImage(imgUrl,function(URI){//Do some thing while image is load});
function loadUrl(uri,callback){if(typeof callback!='function'){callback=function(uri){console.log(uri);}}var xhr = new XMLHttpRequest();xhr.responseType = 'text';xhr.onload = function() {callback(xhr.response);}xhr.open('GET', uri, true);xhr.send();}//使用方法var url = 'http:xxx.do';loadUrl(url,function(data){//Do some thing while image is load});
XMLHttpRequest.responseType:
https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/responseType
如:"document","json","text","blob"
阅读全文
0 0
- 通过XMLHttpRequest加载外部图片文件或数据
- Unity3d加载外部图片文件
- 动态加载外部css或js文件
- android中图片加载使用LruCache缓存到内存或外部文件的功能
- CImageList从外部加载图片文件
- VC 从外部文件中加载图片
- 通过ajax加载外部json文件,并实现加载照片
- Flash 加载外部数据-ajax与图片加载
- Android异步加载数据或图片问题
- 使用javascript动态加载外部css或js文件
- 通过JSON数据加载服务器端图片
- 如何让全局常量通过外部文件properties来加载
- flash加载外部图片
- 加载外部图片
- 外部加载图片自适应
- as2 加载外部图片
- 加载外部图片
- unity3d加载外部图片
- mac JDK9的安装与环境配置以及Tomcat安装错误问题的解决
- 单源最短路径
- 各种Java加密算法
- HTML 面试常见知识点
- iOS广告Banner,使用三个ImgView无限循环轮播
- 通过XMLHttpRequest加载外部图片文件或数据
- CSS面试常见知识点
- JS原型链、继承的问题与解决:组合继承、借用构造函数(未完)
- inux必学的命令
- 打印数字
- SpringMVC视图解析器InternalResourceViewResolver
- OpenJ_Bailian
- given final block not properly padded aes
- JAVA内存报错解析