input file 美化及上傳本地預覽

來源:互聯網
上載者:User

標籤:style   http   io   ar   color   os   使用   sp   java   

 

效果:

 

/*input file 美化及上傳本地預覽,相容IE6-8,FIrefox, Chrome(需在服務端,本地無效)*/
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>input file 美化</title>
    <script src="jquery-1.8.2.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $("input[type = ‘file‘]").change(function () {
                var explorer = window.navigator.userAgent;
                var url = document.getElementById("image");
                var file = document.getElementById("file");
                if (explorer.indexOf("MSIE") >= 0) {
                    file.select();
                    url.src = document.selection.createRange().text;
                }
                else if (explorer.indexOf("Firefox") >= 0 || explorer.indexOf("Chrome") >= 0) {
                    var oFReader = new FileReader();
                    var oFile = file.files[0];
                    oFReader.onload = function (e) {
                        url.src = e.target.result;
                    }
                    oFReader.readAsDataURL(oFile);
                }
            });
        })
    </script>
    <style>
        * { margin: 0; padding: 0; }

        a { text-decoration: none; }

        .btn_addPic {
            display: block;
            position: relative;
            width: 140px;
            height: 39px;
            overflow: hidden;
            border: 1px solid #EBEBEB;
            background: none repeat scroll 0 0 #F3F3F3;
            color: #999999;
            cursor: pointer;
            text-align: center;
        }
        .btn_addPic span { display: block; line-height: 39px; }

        .btn_addPic em {
            background: url(add.png) 0 0;
            display: inline-block;
            width: 18px;
            height: 18px;
            overflow: hidden;
            margin: 10px 5px 10px 0;
            line-height: 20em;
            vertical-align: middle;
        }
        .btn_addPic:hover em { background-position: -19px 0; }

        .filePrew {
            display: block;
            position: absolute;
            top: 0;
            left: 0;
            width: 140px;
            height: 39px;
            font-size: 100px; /* 增大不同瀏覽器的可點擊地區 */
            opacity: 0; /* 實現的關鍵點 */
            filter: alpha(opacity=0); /* 相容IE */
        }

        #min_img { width: 100px; }
        
        #min_img img { width: 100%; }
    </style>
    
</head>
<body>
<form>
    <a href="javascript:void(0);" class="btn_addPic">
        <span><em>+</em>添加圖片</span>
        <input tabindex="3" title="支援jpg、jpeg、gif、png格式,檔案小於5M" size="3" name="file" id="file" class="filePrew" type="file">
    </a>
    <div id="min_img"><img id="image" /></div>
</form>
</body>
</html>

  add.png

 

**********************************************************************************************************************

input file美化原理:將input file透明值設為0(opacity: 0;),就不會顯示,但是佔據位置,可以相應操作,然後在它上面或下面放上美化的元素即可。

**********************************************************************************************************************

1. IE6可以使用value屬性擷取絕對路徑
例:
 $("input[type = ‘file‘]").change(function () {
            var url= $(this).val();
 });

IE7 8本地可以擷取地址,伺服器只能擷取名字,可以使用以下方式擷取,支援IE6-8。
file.select();
url.src = document.selection.createRange().text;

2. Firefox和Google可以使用html5的File API中得FileReader擷取本地路徑

    readAsArrayBuffer(file):將檔案讀取為ArrayBuffer。
    readAsBinaryString(file):將檔案讀取為二進位字串
    readAsDataURL(file):將檔案讀取為Data URL
    readAsText(file, [encoding]):將檔案讀取為文本,encoding預設值為‘UTF-8‘

 

   FileReader對象在讀取檔案後,還需要進行處理。為了不阻塞當前線程,API採用了事件模型,可以註冊這些事件:

    onabort:中斷時觸發
    onerror:出錯時觸發
    onload:檔案成功讀取完畢時觸發
    onloadend:檔案讀取完畢時觸發,無論是否失敗
    onloadstart:檔案開始讀取時觸發
    onprogress:當檔案讀取時,周期性地觸發

 

例:

 $("input[type = ‘file‘]").change(function () {
            var oFReader = new FileReader();
            var oFile = document.getElementById("file").files[0];

            oFReader.onload = function(e) {
                 document.getElementById("image").src = e.target.result;  /*不可傳遞給變數*/
            }
            
            oFReader.readAsDataURL(oFile);
   });


3. Google可以使用window.webkitURL.createObjectURL(file.files[0])擷取本地路徑
例:
 $("input[type = ‘file‘]").change(function () {
            var docObj=document.getElementById("file");
            var imgurl= window.webkitURL.createObjectURL(docObj.files[0]);
 });

註: 2 3Google必須在伺服器端才可有效果,2Firefox本地可用。

 

input file 美化及上傳本地預覽

聯繫我們

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