jquery 圖片上傳預覽外掛程式代碼

來源:互聯網
上載者:User

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<title>jquery 圖片上傳</title>
<script language="網頁特效">
//preview image  
(function($) {  
    $.fn.previewimage = function(options) {  
        var default = {  
            imageclientid: "",  
            maxwidth: 300,  
            maxheight: 300  
        };  
        $.extend(true, default, options);  
        return this.each(function() {  
            if (default.imageclientid != "") {  
                $(this).unbind("change");  
                $(this).change(function() {  
                    if ($(this).val() == "") {  
                        $("#" + default.imageclientid).parent("div").hide();  
                        return;  
                    }  
                    else {  
                        $("#" + default.imageclientid).parent("div").show();  
                    }  
                    if ($.browser.msie) {  
                        $("#" + default.imageclientid).attr("src", $(this).val());  
                    }  
                    else {  
                        $("#" + default.imageclientid).attr("src", $(this)[0].files[0].getasdataurl());  
                    }  
                    if ($.browser.msie && $.browser.version > 6) {  
                        $("#" + default.imageclientid).hide();  
                        $("#" + default.imageclientid).parent("div").css教程({ 'z-index': '999',  
                            'filter': 'progid:dximagetransform.microsoft.alphaimageloader(sizingmethod=scale)',  
                            'max-width': default.maxwidth + 'px', 'max-height': default.maxheight + 'px',  
                            'width': default.maxwidth + 'px', 'height': default.maxheight + 'px' 
                        });  
                        var div = $("#" + default.imageclientid).parent("div")[0];  
                        div.filters.item("dximagetransform.microsoft.alphaimageloader").src = $("#" + default.imageclientid).attr("src");  
                    }  
                });  
 
                $("#" + default.imageclientid).load(function() {  
                    var image = new image();  
                    image.src = $(this).attr("src");  
                    $(this).attr("width", default.maxwidth);  
                    $(this).attr("height", default.maxheight);  
                    $(this).attr("alt", default.maxwidth + "x" + default.maxheight);  
                });  
            }  
        });  
    };  
})(jquery);

</script>
</head>

<body>
<asp教程:fileupload id="fileupload_1" runat="server"  />  
 
 
$("#<%=fileupload_1.clientid %>").previewimage({ imageclientid: "uploadimage", maxwidth: "425", maxheight: "200" }); 
</body>
</html>


圖片上傳預覽外掛程式二

//preview image
(function($) {
    $.fn.previewimage = function(options) {
        var default = {
            imageclientid: "",
            maxwidth: 300,
            maxheight: 300
        };
        $.extend(true, default, options);
        return this.each(function() {
            if (default.imageclientid != "") {
                $(this).unbind("change");
                $(this).change(function() {
                    if ($(this).val() == "") {
                        $("#" + default.imageclientid).parent("div").hide();
                        return;
                    }
                    else {
                        $("#" + default.imageclientid).parent("div").show();
                    }
                    if ($.browser.msie) {
                        $("#" + default.imageclientid).attr("src", $(this).val());
                    }
                    else {
                        $("#" + default.imageclientid).attr("src", $(this)[0].files[0].getasdataurl());
                    }
                    if ($.browser.msie && $.browser.version > 6) {
                        $("#" + default.imageclientid).hide();
                        $("#" + default.imageclientid).parent("div").css({ 'z-index': '999',
                            'filter': 'progid:dximagetransform.microsoft.alphaimageloader(sizingmethod=scale)',
                            'max-width': default.maxwidth + 'px', 'max-height': default.maxheight + 'px',
                            'width': default.maxwidth + 'px', 'height': default.maxheight + 'px'
                        });
                        var div = $("#" + default.imageclientid).parent("div")[0];
                        div.filters.item("dximagetransform.microsoft.alphaimageloader").src = $("#" + default.imageclientid).attr("src");
                    }
                });

                $("#" + default.imageclientid).load(function() {
                    var image = new image();
                    image.src = $(this).attr("src");
                    $(this).attr("width", default.maxwidth);
                    $(this).attr("height", default.maxheight);
                    $(this).attr("alt", default.maxwidth + "x" + default.maxheight);
                });
            }
        });
    };
})(jquery);

使用時在img外面套一個div(相容ie7,ie8,用於加濾鏡dximagetransform.microsoft.alphaimageloader)
例如:
控制項

:<asp:fileupload id="fileupload1" runat="server" />
                    <br />
                    <div style="display: none;">
                        <img id="uploadimage" src="" /></div>
jquery代碼:$(document).ready(function() {
            $("#<%=fileupload1.clientid %>").previewimage({ imageclientid: "uploadimage", maxwidth: "500", maxheight: "500" });
        });

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.