thinkphp3.2.3使用formdata的多文件上傳


使用formdata的多文件上傳  廢話少說 直接上代碼

1 JS部分

//選擇文件后的處理
 function handleFileSelect()
 {
     var exerciseid=$("#exerciseid").val();
     var workerid=$("#workerid").val();
     var pic_kind=$("#pic_kind").val();
     //var pic_file = $('#pic_file')[0].files[0];
     
     var formData = new FormData();

    //formData.append("pic_file",$('#pic_file')[0].files[0]);
   
   var filesize=$('#pic_file')[0].files.size;
   
    alert(filesize);
    
    for(var i=0; i<$('#pic_file')[0].files.length;i++){
        alert($('#pic_file')[0].files[i].size);
        formData.append('file[]', $('#pic_file')[0].files[i]);
    }
    
    formData.append("pic_kind", pic_kind);
    formData.append("exerciseid", exerciseid);
    formData.append("workerid", workerid);
     
     $.ajax({
         url: "{:U('Publishset/newUpload')}",
         dataType:'json',
         type:'POST',
         data: formData,
         processData : false, // 使數據不做處理
         contentType : false, // 不要設置Content-Type請求頭
         xhr: function(){ //獲取ajaxSettings中的xhr對象,為它的upload屬性綁定progress事件的處理函數
             myXhr = $.ajaxSettings.xhr();
             if(myXhr.upload){ //檢查upload屬性是否存在
                 //綁定progress事件的回調函數
                 myXhr.upload.addEventListener('progress',aprogressHandlingFunction, false);
             }
             return myXhr; //xhr對象返回給jQuery使用
         },
         success: function(data){
              
             $('#ap').val(0);
             $('#aprogress').html('');
             
             showExerciseImage(exerciseid);
             //if (data) {
                
               //  alert('上傳成功!');
             //}

         },
         error:function(response){
             console.log(response);
         }
     });

 }
 

 //上傳進度回調函數:
 function aprogressHandlingFunction(e) {
     if (e.lengthComputable) {
         $('#ap').attr({value : e.loaded, max : e.total}); //更新數據到進度條
         var percent = e.loaded/e.total*100;
         $('#aprogress').html(percent.toFixed(2) + "%");
     }
 }
 

2 html 部分

 <input class="file" id="pic_file" multiple type="file" onchange="handleFileSelect(event)" style="display:none;">

3 PHP部分

 public function newUpload()
  {
  
  
      //上傳配置
      $upload = new \Think\Upload();// 實例化上傳類
      $upload->maxSize=3145728 ;// 設置附件上傳大小
      $upload->exts=array('jpg', 'gif', 'png', 'jpeg');// 設置附件上傳類型
      $upload->rootPath='./uploads/inittestimg/'; // 設置附件上傳根目錄
  
      $upload->savePath=''; // 設置附件上傳(子)目錄
      
      //上傳文件
      $info=$upload->upload();
   
      //參數獲取
      $exerciseid=$_POST["exerciseid"];
      $kind=$_POST['pic_kind'];
    $workerid=$_POST['workerid'];
      
   
       
      if(!$info) {// 上傳錯誤提示錯誤信息
          $this->error($upload->getError());
    }else{// 上傳成功 獲取上傳文件信息
        
        $images = M('initimages');
        
        //循環入庫
          foreach($info as $file){
              $imgarr['exerciseid']=$exerciseid;
              $imgarr['src']='./uploads/inittestimg/'.$file['savepath'].$file['savename'];
              $imgarr['kind']=$kind;
              $imgarr['lastreadtime']=time();
 
              //文件名
              //$imgarr['orderid']=str_replace(strrchr($info['pic_file']['name'], "."),"",$info['pic_file']['name']);
              $imgarr['orderid']=$file['name'];
 
              $images->add($imgarr);
          }
                   
      }
   
      echo 1;
  }
   

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM