图片上传即时显示javascript代码
这是基于javascript的一种图片上传即时显示方法,测试结果IE6和火狐浏览器可以正常使用。google浏览器不兼容。
这种方法兼容性比较差,仅供参考,建议使用ajax方法来即时显示图片。
1.首先是javascript代码
/**
* 图片上传即时显示javascript
*/
var allowExt = [ 'jpg', 'gif', 'bmp', 'png', 'jpeg' ];
var preivew = function(file, container) {
try {
var pic = new Picture(file, container);
} catch (e) {
alert(e);
}
}; // 缩略图类定义
var Picture = function(file, container) {
var height = 0, widht = 0, ext = '', size = 0, name = '', path = '';
var self = this;
if (file) {
name = file.value;
if (window.navigator.userAgent.indexOf("MSIE") >= 1) {
file.select();
path = document.selection.createRange().text;
} else if (window.navigator.userAgent.indexOf("Firefox") >= 1) {
if (file.files) {
path = window.URL.createObjectURL(file.files[0]);
} else {
path = file.value;
}
}
} else {
throw "bad file";
}
ext = name.substr(name.lastIndexOf("."), name.length); if (container.tagName.toLowerCase() != 'img') {
throw "container is not a valid img label";
container.visibility = 'hidden';
} container.src = path;
container.alt = name;
container.style.visibility = 'visible';
height = container.height;
widht = container.widht;
size = container.fileSize; this.get = function(name) {
return self[name];
}; this.isValid = function() {
if (allowExt.indexOf(self.ext) !== -1) {
throw 'the ext is not allowed to upload';
return false;
}
}; };
2.html代码,主要在于onchange方法调用上面的javascript方法,然后是一个用来显示图片的img标签
<body>
<input name="image"nchange="preivew(this,document.getElementById('img'));"/>
<img id="img" style="visibility:hidden" height="150px" width="500px">
</body>
图片上传即时显示javascript代码的更多相关文章
- JS兼容各个浏览器的本地图片上传即时预览效果
JS兼容各个浏览器的本地图片上传即时预览效果 很早以前 在工作曾经碰到这么一个需求,当时也是纠结了很久,也是google了很久,没有碰到合适的demo,今天特意研究了下这方面的的问题,所以也就做了个简 ...
- ruby on rails爬坑(三):图片上传及显示
一,问题及思路 最近在用rails + react + mysql基本框架写一个cms + client的项目,里面涉及到了图片的上传及显示,下面简单说说思路,至于这个项目的配置部署,应该会在寒假结束 ...
- 图片上传并显示(兼容ie),图片大小判断
图片上传并显示(兼容ie),图片大小判断 HTML <div id="swf" style="margin: 0 auto;text-align: center;& ...
- JS兼容各个浏览器的本地图片上传即时预览效果\、
在firefox\chrome\ie10等浏览器中可以使用HTML5中的内容实现图片即时预览效果,在IE10以下浏览器中使用滤镜来解决图片显示问题. HTML5中的FileReader对象主要是把文件 ...
- 自学php【一】 任务:图片上传即时可见
工作已经快2周了,头儿给派了个任务做个企业站!这几天正在紧锣密鼓的作战中!等忙完了这个活!写下自己的学习心得体会!与看到文章的您一起分享! 在这里记录每次遇到的难题,如何解决的! 今天要做的功能就是实 ...
- 图片上传和显示——上传图片——上传文件)==ZJ
http://www.cnblogs.com/yc-755909659/archive/2013/04/17/3026409.html aspx上传 http://www.cnblogs.com/mq ...
- struts中用kindeditor实现的图片上传并且显示在页面上
做公司网站的时候由于需要在内容属性中加入图片,所以就有了这个问题,本来一开始找几篇文章看都是讲修改kindeditor/jsp/file_manager_json.jsp和upload_json.js ...
- ueditor图片上传和显示问题
图片上传: 这段是contorller代码 @RequestMapping(value = "/uploadImg", method = RequestMethod.POST) @ ...
- django 图片上传与显示
由于图片上传的需要,学习了一波上传 1. 上传 前端代码 <form action="写上相应的定向位置" method="post" enctype=& ...
随机推荐
- Ehcache(01)——简介、基本操作
http://haohaoxuexi.iteye.com/blog/2112170 目录 1 CacheManager 1.1 构造方法构建 1.2 静态方法构建 2 ...
- CSS里的单位
CSS中预设了16种颜色以及16种颜色的衍生色,这16种颜色是CSS规范推荐的.并且一些主流的浏览器都可以识别.例如以下表所看到的: 十六进制颜色是最经常使用的定义方式.它的基本格式为#RRGGBB, ...
- [Angular 2] How To Debug An Angular 2 Application - Debugging via Augury or the Console
In this lesson we will learn several ways to debug an Angular 2 application, including by using Augu ...
- swift3.0 中NSNotification 的使用
swift3.0 有很大变化,其中之一就是NSNotification使用跟原来不一样,以前NSNotification name是String:3.0中定义了一个类型NSNotification.n ...
- pthread实现多线程查询(转)
导读:大多数网站的性能瓶颈不在PHP服务器上,因为它可以简单地通过横向增加服务器或CPU核数来轻松应对(对于各种云主机,增加VPS或CPU核数就更方便了,直接以备份镜像增加VPS,连操作系统.环境都不 ...
- Cordova 3.0 Plugin 安装 及"git" command line tool is not installed
根据http://docs.phonegap.com/en/edge/guide_cli_index.md.html#The%20Command-line%20Interface Windows命令行 ...
- UNIX标准化及实现之POSIX标准扩展头文件
POSIX标准定义的XSI(X/Open System Interface)扩展头文件 头文件 说明 <cpio.h> cpio归档值 <dlfcn.h> 动态链接 <f ...
- LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation
LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation Evaluate the value of an arithm ...
- 关于php ci框架ie浏览器路径问题
ie不能定位到这个location,而是在地址栏形成类似eg.com/index.php/class/class/class/fucntion (支持应该为eg.com/index.php/class ...
- 小白日记5:kali渗透测试之被动信息收集(四)--theHarvester,metagoofil,meltag,个人专属密码字典--CUPP
1.theHarvester theHarvester是一个社会工程学工具,它通过搜索引擎.PGP服务器以及SHODAN数据库收集用户的email,子域名,主机,雇员名,开放端口和banner信息. ...