移動端壓縮並ajax上傳圖片解決方案

來源:互聯網
上載者:User

標籤:mobile   patch   over   play   pre   log   write   com   success   

1.需求

做一個前端可壓縮並且上傳圖片到背景功能

2.使用組件

用到的主要是jq和LocalResizeIMG這2個庫

3.使用方法

a.引入指令檔

<script type=‘text/javascript‘ src=‘js/jquery-2.0.3.min.js‘></script><script type=‘text/javascript‘ src=‘js/LocalResizeIMG.js‘></script><script type=‘text/javascript‘ src=‘js/patch/mobileBUGFix.mini.js‘></script>

b.編寫html

<div style="width:100%;margin:10px auto; border:solid 1px #ddd; overflow:hidden; ">  <input type="file" id="uploadphoto" name="uploadfile" value="請點擊上傳圖片"   style="display:none;" />   <div class="imglist"></div>   <a href="javascript:void(0);" onclick="uploadphoto.click()" class="uploadbtn">點擊上傳檔案</a></div>

c.編寫css

body{font-family:"微軟雅黑"}*{margin: 0;padding: 0;    }.uploadbtn{ display:block;height:40px; line-height:40px; color:#333; text-align:center; width:100%; background:#f2f2f2; text-decoration:none; }.imglist{min-height:200px;margin:10px;}.imglist img{width:100%;}

d.執行上傳代碼

<script type="text/javascript">$(document).ready(function(e) {   $(‘#uploadphoto‘).localResizeIMG({      //要壓縮到的寬度      width: 1900,      quality: 1,      success: function (result) {       // result.clearBase64是base64的資料          var submitData={                base64_string:result.clearBase64,             };         $.ajax({           type: "POST",           url: "upload.php",           data: submitData,           dataType:"json",           success: function(data){             if (0 == data.status)             {                alert(data.content);                return false;             }             else             {                alert(data.content);                var attstr= ‘<img id=‘+‘element_id‘+‘ src="‘+data.url+‘">‘;                 $(".imglist").append(attstr);                return false;             }           },             complete :function(XMLHttpRequest, textStatus){            },            error:function(XMLHttpRequest, textStatus, errorThrown){ //上傳失敗                alert(XMLHttpRequest.status);               alert(XMLHttpRequest.readyState);               alert(textStatus);            }        });       }  });}); </script>

e.後端php接收資料並處理。

後端會接收前端的base64資料,並base64解碼儲存在images檔案夾下面

<?php     $base64_string = $_POST[‘base64_string‘];    $savename = uniqid().‘.jpeg‘;//localResizeIMG壓縮後的圖片都是jpeg格式    $savepath = ‘images/‘.$savename;     $image = base64_to_img( $base64_string, $savepath );    if($image){        echo ‘{"status":1,"content":"圖片上傳成功,請用手指輕觸要做為頭像的地區","url":"‘.$image.‘"}‘;    }else{        echo ‘{"status":0,"content":"上傳失敗"}‘;    }     function base64_to_img( $base64_string, $output_file ) {        $ifp = fopen( $output_file, "wb" );         fwrite( $ifp, base64_decode( $base64_string) );         fclose( $ifp );         return( $output_file );     } ?>

 

4.總結

思路就是前端把圖片base64編碼,再傳到後台進行base64解碼並儲存把圖片返回給用戶端並在服務端儲存一份。(需在伺服器下運行代碼)

 

參考資料:http://www.cnblogs.com/manongxiaobing/p/4720568.html

移動端壓縮並ajax上傳圖片解決方案

聯繫我們

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