使用html5 FileReader擷取圖片,並非同步上傳到伺服器(不使用iframe),

來源:互聯網
上載者:User

使用html5 FileReader擷取圖片,並非同步上傳到伺服器(不使用iframe),

使用html5 FileReader擷取圖片,並非同步上傳到伺服器(不使用iframe)


原理:

1.使用FileReader 讀取圖片的base64編碼

2.使用ajax,把圖片的base64編碼post到伺服器。

3.根據接收到post的資料分析圖片的類型(jpg,gif,png),並把base64_decode後的資料產生對應類型的圖片檔案。


html:

<!DOCTYPE HTML PUBLIC><html> <head>  <meta charset="utf-8">  <script src="//code.jquery.com/jquery-1.11.0.min.js"></script>  <title>使用html5 FileReader擷取圖片,並非同步上傳到伺服器(not iframe)</title>  <style type="text/css">    body{margin: 0px; background:#f2f2f0;}    p{margin:0px;}    .title{color:#FFFF00; background:#000000; text-align:center; font-size:24px; line-height:50px; font-weight:bold;}    .file{position:absolute; width:100%; font-size:90px;}    .filebtn{display:block; position:relative; height:110px; color:#FFFFFF; background:#06980e; font-size:48px; line-height:110px; text-align:center; cursor:pointer; border: 3px solid #cccccc;}    .filebtn:hover{background:#04bc0d;}    .showimg{margin:10px auto 10px auto; text-align:center;}  </style>  <script type="text/javascript">  window.onload = function(){    // 選擇圖片    document.getElementById('img').onchange = function(){        var img = event.target.files[0];        // 判斷是否圖片        if(!img){            return ;        }        // 判斷圖片格式        if(!(img.type.indexOf('image')==0 && img.type && /\.(?:jpg|png|gif)$/.test(img.name)) ){            alert('圖片只能是jpg,gif,png');            return ;        }        var reader = new FileReader();        reader.readAsDataURL(img);        reader.onload = function(e){ // reader onload start            // ajax 上傳圖片            $.post("server.php", { img: e.target.result},function(ret){                if(ret.img!=''){                    alert('upload success');                    $('#showimg').html('<img src="' + ret.img + '">');                }else{                    alert('upload fail');                }            },'json');        } // reader onload end    }  }  </script> </head> <body>  <p class="title">使用html5 FileReader擷取圖片,並非同步上傳到伺服器(not iframe)</p>  <p><input type="file" class="file" id="img"><label class="filebtn" for="img" title="JPG,GIF,PNG">請選擇圖片</label></p>  <p class="showimg" id="showimg"></p> </body>  </html>

server.php

<?php$img = isset($_POST['img'])? $_POST['img'] : '';// 擷取圖片list($type, $data) = explode(',', $img);// 判斷類型if(strstr($type,'image/jpeg')!==''){    $ext = '.jpg';}elseif(strstr($type,'image/gif')!==''){    $ext = '.gif';}elseif(strstr($type,'image/png')!==''){    $ext = '.png';}// 產生的檔案名稱$photo = time().$ext;// 組建檔案file_put_contents($photo, base64_decode($data), true);// 返回header('content-type:application/json;charset=utf-8');$ret = array('img'=>$photo);echo json_encode($ret);?>





聯繫我們

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