• jquery 图片本地预览


    uploadPreview.js

    /*
    *名称:图片上传本地预览插件 v1.1
    *介绍:基于JQUERY扩展,图片上传预览插件 目前兼容浏览器(IE 谷歌 火狐) 不支持safari
    *参数说明: Img:图片ID;Width:预览宽度;Height:预览高度;ImgType:支持文件类型;Callback:选择文件显示图片后回调方法;
    *使用方法: 
    <div>
    <img id="ImgPr" width="120" height="120" /></div>
    <input type="file" id="up" />
    把需要进行预览的IMG标签外 套一个DIV 然后给上传控件ID给予uploadPreview事件
    $("#up").uploadPreview({ Img: "ImgPr", Width: 120, Height: 120, ImgType: ["gif", "jpeg", "jpg", "bmp", "png"], Callback: function () { }});
    */
    jQuery.fn.extend({
        uploadPreview: function (opts) {
            var _self = this,
                _this = $(this);
            opts = jQuery.extend({
                Img: "ImgPr",
                Width: 100,
                Height: 100,
                ImgType: ["gif", "jpeg", "jpg", "bmp", "png"],
                Callback: function () {}
            }, opts || {});
            _self.getObjectURL = function (file) {
                var url = null;
                if (window.createObjectURL != undefined) {
                    url = window.createObjectURL(file)
                } else if (window.URL != undefined) {
                    url = window.URL.createObjectURL(file)
                } else if (window.webkitURL != undefined) {
                    url = window.webkitURL.createObjectURL(file)
                }
                return url
            };
            _this.change(function () {
                if (this.value) {
                    if (!RegExp(".(" + opts.ImgType.join("|") + ")$", "i").test(this.value.toLowerCase())) {
                        alert("选择文件错误,图片类型必须是" + opts.ImgType.join(",") + "中的一种");
                        this.value = "";
                        return false
                    }
                    if ($.browser.msie) {
                        try {
                            $("#" + opts.Img).attr('src', _self.getObjectURL(this.files[0]))
                        } catch (e) {
                            var src = "";
                            var obj = $("#" + opts.Img);
                            var div = obj.parent("div")[0];
                            _self.select();
                            if (top != self) {
                                window.parent.document.body.focus()
                            } else {
                                _self.blur()
                            }
                            src = document.selection.createRange().text;
                            document.selection.empty();
                            obj.hide();
                            obj.parent("div").css({
                                'filter': 'progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)',
                                'width': opts.Width + 'px',
                                'height': opts.Height + 'px'
                            });
                            div.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = src
                        }
                    } else {
                        $("#" + opts.Img).attr('src', _self.getObjectURL(this.files[0]))
                    }
                    opts.Callback()
                }
            })
        }
    });
    

      

    jsp

    <%@ page language="java" contentType="text/html; charset=utf-8"
    	pageEncoding="utf-8"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <link rel="stylesheet" href="css/common.css">
       <link rel="stylesheet" href="css/main.css">
       <script type="text/javascript" src="js/jquery.min.js"></script>
       <script src="js/uploadPreview.js" type="text/javascript"></script>
    <style type="text/css">
    	.upimage{
    		padding: 6px 10px;
    		height: 35px;
    		line-height: 20px;
    		position: relative;
    		cursor: pointer;
    		color: #888;
    		background: #fafafa;
    		border: 1px solid #ddd;
    		border-radius: 4px;
    		overflow: hidden;
    		display: inline-block;
    		float: left;
    		margin-left: 10px;
    		margin-top: 39px;
    	}
    </style>
    	function choose(){
    		$("#file").show().click();
    		$("#file").uploadPreview({ Img: "ImgPr", Width: 120, Height: 120 });
    	 }
    </script>
    
    
    </head>
    <body>
    	
    		<form action="${pageContext.request.contextPath}/update.do" enctype="multipart/form-data"
    		method="post">
    		<table>
    			
    			<tr>
    					<td  class="td_right">图片:</td>
    					<td class=""> 
    						<div style="float: left; 120px;">
    							<img id="ImgPr" src="${serverPath}/${zySubscription.imgurl}" width="120" height="120" />
    						</div>
    						<input type="button" value="上传图片" onclick="choose();" class="upimage"/> 
    						<div hidden="true">
    							<input type="file" name="file" id="file" hidden="true"/><font color="red">${msg}</font>
    						</div>
    	                  
    	                 </td>
    				</tr>
    			
    			
    		
    
    
    
    
    		</table>
    	</form>
    
    </body>
    </html>
    

      

  • 相关阅读:
    equals 和 == 的区别
    jenkins
    状态码
    对控制反转和依赖注入的突然顿悟
    分布式事务与Seate框架
    synchronized原理
    VS 添加 Sqlserver
    C# 生成二维码
    jQuery /Date(0000000000000)/日期转换
    什么是Java的序列化,在哪些程序中见过Java序列化?
  • 原文地址:https://www.cnblogs.com/zuolun2017/p/5765535.html
Copyright © 2020-2023  润新知