1. 程式人生 > >IE9上傳/預覽圖片(自適應)

IE9上傳/預覽圖片(自適應)

$("input").change(function () {
        var fileObj = $(this)[0];
        var windowURL = window.URL || window.webkitURL;
        var dataURL;
        var $img = $("#preview");
        if (fileObj && fileObj.files && fileObj.files[0]) {
            dataURL = windowURL.createObjectURL(fileObj.files[0]);
            $img.attr('src', dataURL);
        } else {
            //在IE9下,獲取圖片絕對路徑
            var imgObj = document.getElementById("preview");
            var file = document.getElementById("input");
            file.select();
            file.blur();
            var dataURL = document.selection.createRange().text;
            document.selection.empty();
            imgObj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + dataURL + "',sizingMethod=image)";
            //圖片必須顯示出來,才能獲取原圖片的高和寬
            $("#preview").show();
            //獲取原圖片的高和寬
            var nWidth = imgObj.offsetWidth;
            var nHight = imgObj.offsetHeight;
            //按比例設定圖片的寬
            var imgWidth = parseInt(nWidth * (200 / nHight));
            $('.preview_img').css("width",imgWidth);
            imgObj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + dataURL + "',sizingMethod=scale)";
        }
        var options = {
            url: "...",
            success: function (res) {
                //...
            },
            resetForm: true
        };
        $("form").ajaxSubmit(options);

    });