• <noscript id="ecgc0"><kbd id="ecgc0"></kbd></noscript>
    <menu id="ecgc0"></menu>
  • <tt id="ecgc0"></tt>

    base64多圖片上傳如何和后臺進行交互

    base64多圖上傳到后臺

    東西/原料

    • sublime
    • thinkphp

    方式/步調

    1. 1

      起首建立html頁面:

          

        <div class="container">   

                  <label>請選擇一個圖像文件:</label> 

                  <button style="display:none;" id="select">(從頭)選擇圖片</button> 

                  <button id="add">選擇上傳圖片</button> 

                  <input type="file" id="file_input" multiple/> <!--用input標簽并選擇type=file,記得帶上multiple,否則就只能單選圖片了-->   

                  <button id="submit">提交</button>

              </div>   

    2. 2

      <style type="text/css">  

          .float{   

              float:left;   

              width : 200px;   

              height: 200px;  

               overflow: hidden;   

              border: 1px solid #CCCCCC;  

               border-radius: 10px;   

              padding: 5px; 

                margin: 5px;  

           }   

          img{   

              position: relative;  

           }   

          .result{   

              width: 200px;   

              height: 200px;  

               text-align: center;   

              box-sizing: border-box;  

           }

          #file_input{ 

              display: none; 

          }

      </style>

    3. 3

      window.onload = function(){   

          var input = document.getElementById("file_input");   

          var result;   

          var dataArr = []; // 儲存所選圖片的成果(文件名和base64數據) 

          var fd;  //FormData體例發送請求   

          var oSelect = document.getElementById("select"); 

          var oAdd = document.getElementById("add"); 

          var oSubmit = document.getElementById("submit"); 

          var oInput = document.getElementById("file_input"); 

          if(typeof FileReader==='undefined'){   

              alert("抱愧,你的瀏覽器不撐持 FileReader");   

              input.setAttribute('disabled','disabled');   

          }else{   

              input.addEventListener('change',readFile,false); 

            }

          function readFile(){  

              fd = new FormData();

                 var iLen = this.files.length; 

              var index = 0; 

              for(var i=0;i<iLen;i++){ 

                  if (!input['value'].match(/.jpg|.gif|.png|.jpeg|.bmp/i)){  //判定上傳文件格局   

                      return alert("上傳的圖片格局不準確,請從頭選擇");   

                  } 

                  var reader = new FileReader(); 

                  reader.index = i;   

                  fd.append(i,this.files[i]); 

                  reader.readAsDataURL(this.files[i]);  //轉當作base64

                     reader.onload = function(e){ 

                       var imgMsg = this.result ;

                      dataArr.push(imgMsg);  

                      result = '<div class="result"><img src="'+this.result+'" alt=""/></div>';   

                      var div = document.createElement('div'); 

                      div.innerHTML = result;   

                      div['className'] = 'float'; 

                      div['index'] = index;   

                      document.getElementsByTagName('body')[0].appendChild(div);    //插入dom樹   

                      var img = div.getElementsByTagName('img')[0]; 

                      img.onload = function(){   

                          var nowHeight = ReSizePic(this); //設置圖片巨細   

                          this.parentNode.style.display = 'block';   

                          var oParent = this.parentNode;   

                          if(nowHeight){   

                              oParent.style.paddingTop = (oParent.offsetHeight - nowHeight)/2 + 'px';   

                          }   

                      }

                      index++; 

                  }   

              }   

          }  

          function send(){  

              var submitArr = []; 

              for (var i = 0; i < dataArr.length; i++) { 

                  if (dataArr[i]) { 

                      submitArr.push(dataArr[i]); 

                  } 

              } 

              $.ajax({   

                  url : '/home/test/upload', 

                    type : 'post',

                  data : {"image":dataArr},   

                  dataType: 'json',   

                   success : function(data){   

                      if (data == 1) {

                          alert("圖片上傳當作功");

                      }else{

                          alert("圖片上傳掉敗");

                      }

                 }

              })   

          }

          oSelect.onclick=function(){  

              oInput.value = "";   // 先將oInput值清空,不然選擇圖片與前次不異時change事務不會觸發 

              $('.float').remove();  //清空已選圖片 

              dataArr = []; 

               index = 0;    

                   oInput.click();  

          }  

          oAdd.onclick=function(){ 

              oInput.value = "";   // 先將oInput值清空,不然選擇圖片與前次不異時change事務不會觸發 

              oInput.click();  

          }  

          oSubmit.onclick=function(){  

               if(!dataArr.length){   

                  return alert('請先選擇文件');   

              }   

              send();   

          }   

      }              

    4. 4

      function ReSizePic(ThisPic) {   

          var RePicWidth = 200; //這里點竄為您想顯示的寬度值

                var TrueWidth = ThisPic.width; //圖片現實寬度   

          var TrueHeight = ThisPic.height; //圖片現實高度 

              if(TrueWidth>TrueHeight){   

              var reWidth = RePicWidth;//廣大于高 

              ThisPic.width = reWidth;    

              var nowHeight = TrueHeight * (reWidth/TrueWidth); //垂直居中   

               return nowHeight;  //將圖片點竄后的高度返回,供垂直居頂用   

          }else{  

               var reHeight = RePicWidth; //寬小于高    

               ThisPic.height = reHeight; 

            }   

      }   

    5. 5

      后臺數據處置:

      public function upload(){

              foreach ($_POST['image'] as $key => $value) {

                  $up_dir = './upload/';//存放在當前目次的upload文件夾下

                  if (!file_exists($up_dir)) {

                      mkdir($up_dir,0777);

                  }

                  if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $value,$result)) {

                      $type = $result[2];

                      if (in_array($type,array('pjpeg','jpeg','jpg','gif','bmp','png'))) {

                          $new_file = $up_dir.date('YmdHis_').rand(10000,99999).'.'.$type;

                          if (file_put_contents($new_file, base64_decode(str_replace($result[1],'',$value)))) {

                              $img_path = str_replace('../../..', '', $new_file);

                              $re[] += 1;

                          }else{

                              $re[] += 0;

                          }

                      }else{

                          $re[] += 0;

                      }

                  }else{

                        $re[] += 0;      

                             }

              }

              if (count($re) == count($_POST['image'])) {

                  echo 1;

              }else{

                  echo 0;

              }

          }

    執行過程及成果

    1. 1

      起首選擇圖片上傳

    2. 2

      測試上傳一張圖片,點擊提交

    3. 3

      圖片已當作功保留在文件夾中

    4. 4

      選擇多張圖片,點擊提交

    5. 5

      多張圖片已當作功保留在文件夾中

    注重事項

    • 注重圖片保留路徑問題
    • 注重base64轉換問題
    • 發表于 2018-09-03 00:00
    • 閱讀 ( 764 )
    • 分類:其他類型

    你可能感興趣的文章

    相關問題

    0 條評論

    請先 登錄 后評論
    聯系我們:uytrv@hotmail.com 問答工具
  • <noscript id="ecgc0"><kbd id="ecgc0"></kbd></noscript>
    <menu id="ecgc0"></menu>
  • <tt id="ecgc0"></tt>
    久久久久精品国产麻豆