- 浏览: 296054 次
- 性别:
- 来自: 成都
文章分类
- 全部博客 (139)
- J2EE (22)
- JavaScript (15)
- J2SE (5)
- 想…… (2)
- 尚学堂设计模式 (0)
- 数据库 (9)
- 易错的地方 (3)
- ckeditor (2)
- jquery&easyui (7)
- spring (2)
- JSON (0)
- Hibernate (10)
- 上传下载 (4)
- SSH (4)
- Html (7)
- 连接池 (6)
- Tomcat (4)
- CSS (2)
- 算法 (1)
- 设计模式 (2)
- Struts2 (2)
- 好的网站 (1)
- 收集贴 (12)
- JVM (1)
- 工具 (4)
- 心情 (3)
- 找工作 (4)
- 推荐书籍 (2)
- 一步步js (3)
- JAR介绍下载 (3)
- 一步步ssh (0)
- word (1)
- The Definitive Guide, 4th Edition (0)
最新评论
-
Znker:
把classpath错写成classpaht结果卡了半天
org.hibernate.hql.ast.QuerySyntaxException: XX is not mapped [from XX] -
tangjizhong:
[color=orange][/color]212313
图解SSH框架配置步骤 -
GreatCoder:
有一种情况:如果你使用注解配置成 @Entity(name=& ...
org.hibernate.hql.ast.QuerySyntaxException: XX is not mapped [from XX] -
itabclichao:
谢谢分享
org.hibernate.hql.ast.QuerySyntaxException: XX is not mapped [from XX] -
alan0509:
不知道信中 这位兄弟 最后怎样了?
我不想一辈子和代码打交道
不需要后台支持,完全在前台通过js完成的
用到了一个jquery插件 image-upload-preview
下载地址:
http://code.google.com/p/image-upload-preview/
下面的代码是里面的示例:
<html> <head> <title>Image Upload Preview Demo</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <!-- Make sure that we can test against real IE8 --> <meta http-equiv="X-UA-Compatible" content="IE=8"/> <script src="imageuploadpreview.js"></script> <style type="text/css"> body { margin: 20px; font: 80% / normal 'arial', 'sans-serif'; } #upload { border: solid 3px #ccc; } .preview-image { display: block; margin: 10px 0; border: solid 3px #aaa; padding: 1px; background: #fff; } </style> </head> <body> <h1>Image Upload Preview Demo</h1> <p>Now's it's only compatible with IE6, IE7, IE8, and Firefox 3</p> <p> <a href="http://code.google.com/p/image-upload-preview/">Google code project home </a> </p> <form> <input type="file" id="upload" style="width:400px; padding:3px;"/> <div id="file-info"></div> </form> <script type="text/javascript"> (function() { var demo = new ImageUploadPreview( // Upload Input Element document.getElementById('upload'), // onPreviewSuccess handler. function(imgInfo) { var info = []; for (var i in imgInfo) { info.push('<li>', i, ' = ', imgInfo[i], '</li>'); } if (info.length) { info.unshift('<ul>'); info.push('</ul>'); } this.getImageElement().className = 'preview-image'; document.getElementById('file-info').innerHTML = info.join(''); }, // onPreviewFail handler. function() { this.getImageElement().className = ''; }); demo.setMaxImageSize(demo.getInputElement().offsetWidth, 300); // If the value already exists, try display image. demo.preview(); })(); </script> </body> </html>
调整file的宽度,就可以调整预览图片的宽度
/** * @fileoverview * JavaScript Image Upload Preview. * Tested and compatible with IE6, IE7, IE8, Firefox 3. * * @author Hedger Wang (hedgerwang@gmail.com) * */ /** * @constructor * @param {HTMLInputElement|String} input * @param {Function?} opt_onSuccess * @param {Function?} opt_onFail */ function ImageUploadPreview(input, opt_onSuccess, opt_onFail) { this.construct(input, opt_onSuccess, opt_onFail); } /** * Empty image that shows either for Http:// or Https://. * @define {String} */ ImageUploadPreview.BLANK_IMAGE_SRC = '//www.google.com/images/cleardot.gif'; /** * @define {RegExp} */ ImageUploadPreview.BASE64_IMG_URL_PATTERN = /^data:image\/((png)|(gif)|(jpg)|(jpeg)|(bmp));base64/i; /** * @type {HTMLInputElement} * @private */ ImageUploadPreview.prototype.input_ = null; /** * @type {Function} * @private */ ImageUploadPreview.prototype.onChangeHandler_ = null; /** * @type {Function} * @private */ ImageUploadPreview.prototype.onPreviewSuccessHandler_ = null; /** * @type {Function} * @private */ ImageUploadPreview.prototype.onPreviewFailHandler_ = null; /** * @type {HTMLImageElement} * @private */ ImageUploadPreview.prototype.image_ = null; /** * @private * @type {boolean} */ ImageUploadPreview.prototype.isCompatible_ = null; /** * @private * @type {Number} */ ImageUploadPreview.prototype.maxWidth_ = 200; /** * @private * @type {Number} */ ImageUploadPreview.prototype.maxHeight_ = 200; /** * @param {HTMLInputElement|String} input * @param {Function?} opt_onSuccess * @param {Function?} opt_onFail * @public */ ImageUploadPreview.prototype.construct = function(input, opt_onSuccess, opt_onFail) { if (typeof input == 'string') { input = document.getElementById(input); } this.onPreviewFailHandler_ = opt_onFail; this.onPreviewSuccessHandler_ = opt_onSuccess; this.input_ = input; this.bindEvents_(); this.image_ = this.createImage_(); }; /** * @public */ ImageUploadPreview.prototype.dispose = function() { var fn = this.onChangeHandler_; // Already disposed. if (!fn) return; var el = this.input_; if (el.addEventListener) { el.removeEventListener('change', fn, false); } else if (el.attachEvent) { el.detachEvent('onchange', fn); } this.onChangeHandler_ = null; this.input_ = null; this.image_ = null; }; /** * @public */ ImageUploadPreview.prototype.preview = function() { var that = this; var onLoad = function(imgInfo) { // Do thing now, maybe do something later. that.maybeCallFunction_(that.onPreviewSuccessHandler_, imgInfo); }; var onError = function(src) { if (!tryLoad()) { that.showEmptyImage_(); that.maybeCallFunction_(that.onPreviewFailHandler_, src); } }; var loadMethods = [ this.maybeShowImageWithDataUri_, this.maybeShowImageByPath_ ]; var tryLoad = function() { if (!loadMethods.length) { return false; } var fn = loadMethods.shift(); fn.call(that, onLoad, onError); return true; }; tryLoad(); }; /** * @public * @return {HTMLImageElement} */ ImageUploadPreview.prototype.getImageElement = function() { return this.image_; }; /** * @public * @return {HTMLInputElement} */ ImageUploadPreview.prototype.getInputElement = function() { return this.input_; }; /** * @public * @param {Number} maxW * @param {Number} maxH */ ImageUploadPreview.prototype.setMaxImageSize = function(maxW, maxH) { this.maxHeight_ = isNaN(maxH) ? 10000 : maxH; this.maxWidth_ = isNaN(maxW) ? 10000 : maxW; }; /** * @private * @return {HTMLImageElement} */ ImageUploadPreview.prototype.createImage_ = function() { var doc = this.input_.document || this.input_.ownerDocument; var img = doc.createElement('img'); img.src = ImageUploadPreview.BLANK_IMAGE_SRC; img.width = 0; img.height = 0; this.input_.parentNode.insertBefore(img, this.input_.nextSibling || null); return img; }; /** * @private */ ImageUploadPreview.prototype.bindEvents_ = function() { var that = this; var fn = this.onChangeHandler_ = function(e) { e = e || window.event; if (!e.target && e.srcElement) { e.target = e.srcElement; } that.handleOnchange_.call(that, e); }; var el = this.input_; if (el.addEventListener) { el.addEventListener('change', fn, false); } else if (el.attachEvent) { el.attachEvent('onchange', fn); } }; /** * @param {Event} e * @private */ ImageUploadPreview.prototype.handleOnchange_ = function(e) { this.preview(); }; /** * @private */ ImageUploadPreview.prototype.showEmptyImage_ = function() { this.showImage_(ImageUploadPreview.BLANK_IMAGE_SRC, 0, 0) }; /** * @private * @param {string} src * @param {number} w * @param {number} h */ ImageUploadPreview.prototype.showImage_ = function(src, w, h) { if (w > h) { if (w > this.maxWidth_) { h = h * this.maxWidth_ / w; w = this.maxWidth_; } } else { if (h > this.maxHeight_) { w = w * this.maxHeight_ / h; h = this.maxHeight_; } } var img = this.image_; img.src = src; var imgStyle = img.style; imgStyle.maxHeight = this.maxHeight_ + 'px'; imgStyle.maxWidth = this.maxWidth_ + 'px'; imgStyle.width = (w >= 0) ? Math.round(w) + 'px' : 'auto'; imgStyle.height = (h >= 0) ? Math.round(h) + 'px' : 'auto'; }; /** * @param {Function?} fn * @param {Object?} var_args */ ImageUploadPreview.prototype.maybeCallFunction_ = function(fn, var_args) { if (typeof fn != 'function') return; var_args = Array.prototype.slice.call(arguments, 1); fn.apply(this, var_args); }; /** * Note: Only Firefox 3 can do file.getAsDataURL() by 6/1/2009. * See {@link https://developer.mozilla.org/En/NsIDOMFile}. * @private * @param {Function?} opt_onload * @param {Function?} opt_onerror */ ImageUploadPreview.prototype.maybeShowImageWithDataUri_ = function(opt_onload, opt_onerror) { var el = this.input_; var file = el.files && el.files[0]; var src; var fileName = el.value; // Check if we can access the serialized file via getAsDataURL(). if ((file && file.getAsDataURL) && (src = file.getAsDataURL()) && (ImageUploadPreview.BASE64_IMG_URL_PATTERN.test(src))) { var that = this; var img = this.image_; if ('naturalWidth' in this.image_) { // Firefox has naturalWidth. this.image_.src = src; setTimeout(function() { that.showImage_(src, img.naturalWidth, img.naturalHeight); that.maybeCallFunction_(opt_onload, { width: img.naturalWidth, height: img.naturalHeight, fileName : fileName, fileSize: el.files[0].fileSize }); }, 10); } else { // Use default CSS max-width / max-height to render the size. that.showImage_(src, -1, -1); this.maybeCallFunction_(opt_onload, { fileName : fileName, width: img.offsetWidth, height: img.offsetHeight, fileSize: el.files[0].fileSize }); } } else { this.maybeCallFunction_(opt_onerror, fileName); } }; /** * Note: IE6 ~ IE8 can access image with local path. By 6/1/2009. * However, this may still not work if the security setting changes. * @private * @param {Function?} opt_onload * @param {Function?} opt_onerror */ ImageUploadPreview.prototype.maybeShowImageByPath_ = function(opt_onload, opt_onerror) { var that = this; var el = this.input_; var img = new Image(); var timer; var fileName = el.value.split(/[\\\/]/ig).pop(); var dispose = function() { if (timer) { window.clearInterval(timer); } img.onload = null; img.onerror = null; timer = null; dispose = null; img = null; that = null; checkIsComplete = null; handleError = null; handleComplete = null; }; // Handle the case whether the File is not a image file or the path is not a // valid path to access. var handleError = function() { that.maybeCallFunction_(opt_onerror, el.value); dispose(); }; var handleComplete = function() { var w = img.width; var h = img.height; that.showImage_(img.src, w, h); that.maybeCallFunction_(opt_onload, { src:fileName, width: w, height: h, fileSize: img.fileSize // Note that FileSize is an IE's only attribute. }); dispose(); }; var checkIsComplete = function(e) { e = e || window.event; var type = e && e.type; if (type == 'error') { // If the onError event is called. handleError(); } else if (img.complete || type == 'load') { // Sometimes IE does not fire "onload" event if the image was cahced. // So we have to check the "complete" state to know whether it's ready. if (!img.width || !img.height) { // Even it's not a real image, the onload event may still gets fired. // Check if its width or height is 0. If true, it's not a real image. handleError(); } else { handleComplete(); } } }; img.onload = checkIsComplete; img.onerror = checkIsComplete; timer = window.setInterval(checkIsComplete, 50); // In IE, el.value us the full path of the file rather than just the fileName, // and we'd test if we can load image from this path. img.src = el.value; };
- 图片预览.rar (3.7 KB)
- 下载次数: 183
发表评论
-
javascript权威指南,英文第四版
2011-08-05 10:22 789。。 -
onchange和onpropertychange
2011-02-13 17:15 962从网上转来的,哪转的不知道了…… 可是实时显示用户的输入 ... -
ie之浏览器不支持此方法
2011-01-23 19:54 900系统是win7,浏览器是系统自带的,js一直报浏览器不支持此方 ... -
js中prototype用法
2011-01-19 13:35 749转自: http://apps.hi.baidu ... -
jquery.form上传文件,不执行seccess函数,接收不到返回的json的解决办法
2011-01-12 13:32 4976js代码 $(function() { //初始化 ... -
search方法
2011-01-10 17:30 993转自: http://www.fzs8.ne ... -
js验证用户输入必须为数字,且验证位数
2011-01-08 14:33 10702function checkNum(num) { //验 ... -
jquery获取radio的值
2010-10-31 15:51 1373<input type="radio" ... -
jquery为控件动态绑定事件
2010-10-24 18:03 1805$("#username").bind({ ... -
清空文件选择框的内容
2010-10-24 17:59 1073//取消选择的照片 function canclePhoto ... -
判断用户选择的文件类型是否符合要求
2010-10-24 17:58 826转载自脚本之家 //检查用户上传的是否为照片 ... -
jquery处理任意多个元素,判断任意个文本框的值是否被修改
2010-10-11 19:45 2987预想使用focus等事件,考虑到浏览器不支持的缘故,便使用了 ... -
常用JavaScript函数
2010-09-18 22:15 696返回上一页面 function goBack() { ... -
js获取字符串字节数
2010-09-18 14:25 2150非原创,转载自脚本之家 //获取字符串字节数 funct ...
相关推荐
NULL 博文链接:https://huangjiateng.iteye.com/blog/1194870
虽然可以在ie6,ie7,火狐预览、但在谷歌浏览器还是不行、哪位高人要是有更好的办法浏览、能否交流交流. qq:253930407 email:ruanjingwang@yahoo.com.cn
图片上传 预览 兼容 IE 6/7/8 firefox / 新版firefox
asp.net 多图片上传 预览 兼容火狐 IE
我找了很久,大多数都不能兼容最新版的火狐,这个能行,希望可以帮助到真正需要的人
兼容ie[6-9]、火狐、Chrome、opera、maxthon3、360浏览器的js本地图片预览
Jsp图片预览程序(含Java源码) 目前,很多网站都在为提升用户体验而努力,想尽多种办法让用户在网站上...程序中的JavaScript部分兼容ie6/7/8, firefox 3.5.5以及opera 10、safari 4.0.4、 chrome 3.0 等多种浏览器。
JavaScript解决预览上传问题,目前支持IE7、IE8、火狐浏览器; 上网查找了大量资料,然而能用的代码却寥寥无几;竟然弄出来了,就共享给大家。
js上传前预览图片,兼容IE,firefox,google
javascript 上传并预览图片(兼容IE,6,7,8,9+,谷歌,火狐,Safari)移动端也可以。
2 兼容ie,firefox 3 实现判断文件大小。火狐下实现了上传前判断图片大小,ie下目前是采用ActiveXObject方法,但会弹框提示,不友好,提供ajax方式后台判断文件大小。 4 判断图片类型合法性。 5 上传前判断图片尺寸。...
NULL 博文链接:https://youyou-1987.iteye.com/blog/813465
js预览图片兼容ie6,7,8,火狐等浏览器
图片上传预览是一种在图片上传之前对图片进行本地预览的技术。 使用户选择图片后能立即查看图片,而不需上传服务器,提高...兼容:ie6/7/8, firefox 3.5.5 后台支持下还兼容:opera 10.10, safari 4.0.4, chrome 3.0
1. 解压 PDF前端预览功能.zip 压缩包,复制到网站开发目录; 2. 调用方式:window.open("Scripts/lib/PDF/web/viewer.... 3. 能支持多种浏览,火狐、IE、谷歌、360等,能支持本地文件直接浏览。
<body> <form id="form1" runat="server"> ; height: 50px;"> *封面: ... ID="FileUpload1" runat="server" />请上传案例的封面图片 </form> </body>
图片上传预览,亲测兼容:ie6~ie8及以上浏览器,谷歌,火狐,Safari,opera,兼容移动端
NULL 博文链接:https://1017401036.iteye.com/blog/2098990
主要介绍了兼容最新firefox、chrome和IE的javascript图片预览实现代码,测试了浏览器firefox6、firefox12、chrome 25.0.1364.172 m、IE6-IE10 都兼容,需要的朋友可以参考下