頁面上傳圖片後立即預覽

來源:互聯網
上載者:User

標籤:

      頁面上的應用大多都有在個人資訊管理中設定頭像的這麼一個功能,而對於實現這個功能前提就是上傳頭像的功能,而當上傳完成即可預覽是所有後續操作的前提。在網上巴拉了一番,然後整理了一下,拼湊了一個小demo供大家學習。本博文出自部落格園,作者Red,聯絡郵箱 [email protected],轉載請保留本文原文連結http://www.cnblogs.com/itred/p/5723864.html。

      這個demo基本可以實現頁面上開啟頁面後,點擊上傳頭像圖片的按鈕,經過選擇,上傳成功後即可在頁面指定位置顯示剛剛確認上傳完成的圖片,這個圖片的大小是經過預設設定的,對於頁面的樣式,圖片的大小等這些都可以根據自己的實際應用進行調整,最重要的是這個功能基本可用,而且相容多個瀏覽器,不會出現瀏覽器不相容的問題,如果還有瀏覽器沒有測出來,請各位指出,以便進一步最佳化,在此表示感謝。

     說一下開發環境,我用的是idea完成的,但是這個demo並不依賴開發環境,實際上就是一個html和一張預設的圖片,其實就是最基礎的js實現的。因此一般人都可以看懂,而且還可以進一步進行擴充。

1.style樣式設定:

<title>圖片上傳本地預覽</title><style type="text/css">    #preview{        width: 180px;        height: 183px;        border: 1px solid gray;        overflow: hidden;    }    #imghead{        filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=image);    }</style>

2. 核心javascript:

<script type="text/javascript">    function previewImage(file) {        var MAXWIDTH = 180;        var MAXHEIGHT = 180;        var div = document.getElementById(‘preview‘);        if (file.files && file.files[0]) {            div.innerHTML = ‘<img id=imghead>‘;            var img = document.getElementById(‘imghead‘);            img.onload = function () {                var rect = clacImgZoomParam(MAXWIDTH, MAXHEIGHT, img.offsetWidth, img.offsetHeight);                img.width = rect.width;                img.height = rect.height;                img.style.marginLeft = rect.left+‘px‘;                img.style.marginTop = rect.top + ‘px‘;            }            var reader = new FileReader();            reader.onload = function (evt) {                img.src = evt.target.result;            }            reader.readAsDataURL(file.files[0]);        }else{  //相容IE            var sFilter=‘filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale,src="‘;            file.select();            var src = document.selection.createRange().text;            div.innerHTML = ‘<img id=imghead>‘;            var img = document.getElementById(‘imghead‘);            img.filters.item(‘DXImageTransform.Microsoft.AlphaImageLoader‘).src = src;            var rect = clacImgZoomParam(MAXWIDTH, MAXHEIGHT, img.offsetWidth, img.offsetHeight);            status =(‘rect:‘+rect.top+‘,‘+rect.left+‘,‘+rect.width+‘,‘+rect.height);            div.innerHTML = "<div id=divhead style=‘width:"+rect.width+"px;height:"+rect.height+"px;margin-top:"+rect.top+"px;"+sFilter+src+"\"‘></div>";        }    }    function clacImgZoomParam( maxWidth, maxHeight, width, height ){        var param = {top:0, left:0, width:width, height:height};        if( width>maxWidth || height>maxHeight ){            rateWidth = width / maxWidth;            rateHeight = height / maxHeight;            if( rateWidth > rateHeight ){                param.width =  maxWidth;                param.height = Math.round(height / rateWidth);            }else{                param.width = Math.round(width / rateHeight);                param.height = maxHeight;            }        }        param.left = Math.round((maxWidth - param.width) / 2);        param.top = Math.round((maxHeight - param.height) / 2);        return param;    }</script>

3. body布局如下:

<body><div id="preview">    <img id="imghead" border=0 src="img/head_180.jpg" width="180" height="180" /></div><input type="file" onchange="previewImage(this)" /></body>

 

 基本就是完成了。demo的源碼下載請點擊我,運行效果如下:

      

  Red 部落格:http://itred.cnblogs.com
  GitHub:https://github.com/itRed

著作權聲明:本文著作權歸作者和部落格園共有,歡迎轉載,但未經作者同意必須保留此段說明,
且在文章明顯位置給出原文連結,否則保留追究法律責任的權利。

 

頁面上傳圖片後立即預覽

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.