DIV CSS 佈局教程網

JavaScript 實現上傳圖片並即時預覽的兩種寫法
編輯:關於JavaScript     

「選好圖片就能看到預覽」是很貼心的使用者體驗。以下介紹兩種做法:一種利用 IE 的 AlphaImageLoader 濾鏡,另一種則相容 IE6/7/8 與 Firefox。

做法一:利用 AlphaImageLoader 濾鏡

這裡的主角是 AlphaImageLoader 濾鏡的 src 屬性,它能用絕對或相對 URL 指定背景圖像,若忽略此參數,濾鏡便不會作用。

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "https://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<title></title>
<style type="text/css">
#picshow
{
    filter:progid:dximagetransform.microsoft.alphaimageloader(sizingmethod=scale);
    width:88px;
    height:125px;
}
</style>
<script type="text/javascript" language="javascript">
<!--
function upimg(imgfile)
{
    var picshow = document.getElementById("picshow");
    picshow.filters.item("dximagetransform.microsoft.alphaimageloader").src = imgfile.value;
    picshow.style.width = "88px";
    picshow.style.height = "125px";
}
//-->
</script>
</head>
<body>
<div id="picshow"></div>
<p>選擇圖片:<input type="file" size="20" onchange="upimg(this);" /></p>
</body>
</html>

做法二:同時相容 IE6/7/8 與 Firefox

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en"
"https://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<script>
var picpath;
var image;
// preview picture
function preview()
{
    document.getElementById('preview').style.display = 'none';
    // 下面代碼用來獲得圖片尺寸,這樣才能在 ie 下正常顯示圖片
    document.getElementById('box').innerHTML
        = "<img width='"+ image.width +"' height='"+ image.height +"' id='apic' src='"+ picpath +"'>";
}
// show view button
function buttonshow()
{
    /*
     * 這裡用來解決圖片載入延時造成的預覽失敗。
     * 當 image 物件的 src 屬性改變時,javascript 會重新載入圖片內容,
     * 這通常需要一些時間,若在載入完成前就想顯示圖片便會出錯。
     * 因此透過圖片的寬高判斷圖片是否已成功載入,載入完畢才顯示預覽按鈕。
     */
    if ( image.width == 0 || image.height == 0 ) {
        setTimeout(buttonshow, 1000);
    } else {
        document.getElementById('preview').style.display = 'block';
    }
}
function loadimage(ele) {
    picpath = getpath(ele);
    image = new Image();
    image.src = picpath;
    setTimeout(buttonshow, 1000);
}
function getpath(obj)
{
    if(obj)
    {
        // ie
        if (window.navigator.userAgent.indexOf("msie") >= 1)
        {
            obj.select();
            // ie 下取得圖片的本地路徑
            return document.selection.createRange().text;
        }
        // firefox
        else if(window.navigator.userAgent.indexOf("firefox") >= 1)
        {
            if(obj.files)
            {
                // firefox 下取得的是圖片的數據
                return obj.files.item(0).getAsDataURL();
            }
            return obj.value;
        }
        return obj.value;
    }
}
</script>
</head>
<body>
<input type="file" name="pic" id="pic" onchange='loadimage(this)' />
<input id='preview' type='button' value='preview' style='display:none;' onclick='preview();'>
<div id='box'></div>
</body>
</html>

第二種做法的關鍵在於 getpath 函式:IE 透過 document.selection.createRange().text 取得圖片的本地路徑,Firefox 則用 obj.files.item(0).getAsDataURL() 取得圖片數據。此外,buttonshow 透過反覆檢查圖片的寬高,解決了圖片載入延時導致預覽失敗的問題——只有確認圖片載入完成後,才顯示預覽按鈕。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved