主頁 > 知識庫 > TP5框架實現(xiàn)一次選擇多張圖片并預(yù)覽的方法示例

TP5框架實現(xiàn)一次選擇多張圖片并預(yù)覽的方法示例

熱門標(biāo)簽:Linux服務(wù)器 服務(wù)器配置 電子圍欄 銀行業(yè)務(wù) Mysql連接數(shù)設(shè)置 科大訊飛語音識別系統(tǒng) 阿里云 團(tuán)購網(wǎng)站

本文實例講述了TP5框架實現(xiàn)一次選擇多張圖片并預(yù)覽的方法。分享給大家供大家參考,具體如下:

點擊選擇圖片(可選多張),確定后將選擇的圖片顯示在頁面上,已經(jīng)選擇的圖片也可以刪除,點擊提交將圖片提交給后臺。

1、效果圖

2、code

用input標(biāo)簽并選擇type=file,記得帶上multiple,不然就只能單選圖片了

如果不想通過 ajax 提交,一定要加上文件傳輸協(xié)議 ( enctype="multipart/form-data" )

view

!DOCTYPE html>
html>
head>
  meta charset="UTF-8">
  title>showImages/title>
  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;
    }
    .delete{
      width: 200px;
      height:200px;
      position: absolute;
      text-align: center;
      line-height: 200px;
      z-index: 10;
      font-size: 30px;
      background-color: rgba(255,255,255,0.8);
      color: #777;
      opacity: 0;
      transition-duration: :0.7s;
      -webkit-transition-duration: 0.7s;
    }
    .delete:hover{
      cursor: pointer;
      opacity: 1;
    }
  /style>
  script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js">/script>
  script type="text/javascript">
    window.onload = function(){
      var input = document.getElementById("file_input");
      var result;
      var dataArr = []; // 儲存所選圖片的結(jié)果(文件名和base64數(shù)據(jù))
      var fd; //FormData方式發(fā)送請求
      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);
      }     //handler

      function readFile(){
        fd = new FormData();
        var iLen = this.files.length;
        var index = 0;
        for(var i=0;iiLen;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]); //轉(zhuǎn)成base64
          reader.fileName = this.files[i].name;

          reader.onload = function(e){
            var imgMsg = {
              name : this.fileName,//獲取文件名
              base64 : this.result  //reader.readAsDataURL方法執(zhí)行完后,base64數(shù)據(jù)儲存在reader.result里
            }
            dataArr.push(imgMsg);
            result = 'div class="delete">delete/div>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); //設(shè)置圖片大小
              this.parentNode.style.display = 'block';
              var oParent = this.parentNode;
              if(nowHeight){
                oParent.style.paddingTop = (oParent.offsetHeight - nowHeight)/2 + 'px';
              }
            }

            div.onclick = function(){
              this.remove();         // 在頁面中刪除該圖片元素
              delete dataArr[this.index]; // 刪除dataArr對應(yīng)的數(shù)據(jù)

            }
            index++;
          }
        }
      }

      function send(){
        var submitArr = [];
        for (var i = 0; i  dataArr.length; i++) {
          if (dataArr[i]) {
            submitArr.push(dataArr[i]);
          }
        }
        // console.log('提交的數(shù)據(jù):'+JSON.stringify(submitArr))
        $.ajax({
          url : 'http://39.106.182.218',
          type : 'post',
          data : JSON.stringify(submitArr),
          dataType: 'json',
          //processData: false,  用FormData傳fd時需有這兩項
          //contentType: false,
          success : function(data){
            console.log('返回的數(shù)據(jù):'+JSON.stringify(data))
          }

        })
      }

      oSelect.οnclick=function(){
        oInput.value = "";  // 先將oInput值清空,否則選擇圖片與上次相同時change事件不會觸發(fā)
        //清空已選圖片
        $('.float').remove();
        dataArr = [];
        index = 0;
        oInput.click();
      }

      oAdd.οnclick=function(){
        oInput.value = "";  // 先將oInput值清空,否則選擇圖片與上次相同時change事件不會觸發(fā)
        oInput.click();
      }

      oSubmit.οnclick=function(){
        if(!dataArr.length){
          return alert('請先選擇文件');
        }
        send();
      }
    }
    /*
     用ajax發(fā)送fd參數(shù)時要告訴jQuery不要去處理發(fā)送的數(shù)據(jù),
     不要去設(shè)置Content-Type請求頭才可以發(fā)送成功,否則會報“Illegal invocation”的錯誤,
     也就是非法調(diào)用,所以要加上“processData: false,contentType: false,”
     * */

    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;
      }
    }
  /script>
/head>
body>
div class="container">
  label>請選擇一個圖像文件:/label>
  button id="select">(重新)選擇圖片/button>
  button id="add">(追加)圖片/button>

form action="" method="post" enctype="multipart/form-data">
  input type="file" id="file_input" name="image[]" multiple/>
  !--用input標(biāo)簽并選擇type=file,記得帶上multiple,不然就只能單選圖片了-->
  button id="submit">提交/button>
/form>

/div>
/body>
/html>

controller

$image=request()->file('image');
print_r($image);

3、over?。?!

更多關(guān)于thinkPHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《ThinkPHP入門教程》、《thinkPHP模板操作技巧總結(jié)》、《ThinkPHP常用方法總結(jié)》、《codeigniter入門教程》、《CI(CodeIgniter)框架進(jìn)階教程》、《Zend FrameWork框架入門教程》及《PHP模板技術(shù)總結(jié)》。

希望本文所述對大家基于ThinkPHP框架的PHP程序設(shè)計有所幫助。

您可能感興趣的文章:
  • Spring-boot結(jié)合Shrio實現(xiàn)JWT的方法
  • JAVA集合框架Map特性及實例解析
  • yii框架結(jié)合charjs統(tǒng)計上一年與當(dāng)前年數(shù)據(jù)的方法示例
  • yii框架結(jié)合charjs實現(xiàn)統(tǒng)計30天數(shù)據(jù)的方法
  • thinkphp5 框架結(jié)合plupload實現(xiàn)圖片批量上傳功能示例
  • TP5框架實現(xiàn)上傳多張圖片的方法分析
  • Apache Shrio安全框架實現(xiàn)原理及實例詳解

標(biāo)簽:衡水 蚌埠 萍鄉(xiāng) 江蘇 大理 廣元 棗莊 衢州

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《TP5框架實現(xiàn)一次選擇多張圖片并預(yù)覽的方法示例》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266