首页 > 数据库 > Yii CUploadedFile带验证的多文件上传
2014
11-24

Yii CUploadedFile带验证的多文件上传


  1. <div id="upForms">  
  2. <form id="fileitemdiv1" action="<?php echo $this->createUrl('repairUpload'); ?>" method="post" enctype="multipart/form-data" target="upload_target">  
  3.     <input type="file" name="repair_attached_file1" />  
  4.      <input type="submit" name="submitBtn" value='立即上传' />  
  5.     <span id="upload_repairinfo_success1" style="color:red;"></span>  
  6.     <input type="hidden" name="selectedIndex" value="1" />  
  7.     <!-- 记录上传成功后的id -->  
  8.     <input type="hidden" name="upload_save_to_db_id" id="upload_save_to_db_id1" value="0" />  
  9. </form>  
  10. <iframe id="upload_target" name="upload_target" src="#" style="width:0;height:0;border:0px solid #fff;"></iframe>  
  11. </div>  
  12. <div>  
  13.     <input type="button" value="增加附件" onclick="addfile();">  
  14.     <input type="hidden" id="up_success_file_ids" />  
  15. </div>  


JS

[html]
  1. var filecount=1;  
  2. // 新增一个上传文件控件  
  3. function addfile(){  
  4.     var filediv = document.getElementById("upForms");  
  5.     var fileitemdiv = document.createElement("form");  
  6.     filecount++;  
  7.     var content = "<input type=file name=repair_attached_file"+  
  8.     filecount + ">  <input type=submit name=submitBtn value='立即上传' />  <a href='javascript:removefile("+  
  9.     filecount + ");'>删除</a>  <span id=upload_repairinfo_success"+  
  10.     filecount + " style='color:red;'></span><input type=hidden value="+  
  11.     filecount + " name=selectedIndex /> <input type=hidden name=upload_save_to_db_id id=upload_save_to_db_id"+  
  12.     filecount + " value=0 />";  
  13.   
  14.     fileitemdiv.id       = "fileitemdiv"+filecount;  
  15.     fileitemdiv.method   = "post";  
  16.     fileitemdiv.enctype  = "multipart/form-data";  
  17.     fileitemdiv.target   = "upload_target";  
  18.     fileitemdiv.action   = "<?php echo $this->createUrl('repairUpload'); ?>";  
  19.     fileitemdiv.innerHTML = content;  
  20.     filediv.appendChild(fileitemdiv);  
  21. }  
  22.   
  23. //删除指定上传文件控件  
  24. function removefile(fileIndex){  
  25.     var filediv = document.getElementById("upForms");  
  26.     var fileitemdiv = document.getElementById("fileitemdiv"+fileIndex);  
  27.     filediv.removeChild(fileitemdiv);  
  28. }  
  29.   
  30. //回调成功  
  31. function successUpload(responseText,id,fileIndex){  
  32.     // 1,获取值  
  33.     var ids = document.getElementById("up_success_file_ids").value;  
  34.     if(ids){  
  35.         document.getElementById("up_success_file_ids").value = ids+','+id;  
  36.     }else{  
  37.         document.getElementById("up_success_file_ids").value = id;  
  38.     }  
  39.       
  40.     // 2,本次上传成功,则覆盖之前上传成功的文件  
  41.     document.getElementById("upload_save_to_db_id"+fileIndex).value = id;  
  42.       
  43.     // 3,提示上传成功  
  44.     var spanObj = document.getElementById("upload_repairinfo_success"+fileIndex);  
  45.     //spanObj.innerHTML = "上传成功";  
  46.     spanObj.innerHTML = responseText;  
  47. }  
  48.   
  49. //回调失败  
  50. function stopUpload(responseText,fileIndex){  
  51.     // 提示  
  52.     var spanObj = document.getElementById("upload_repairinfo_success"+fileIndex);  
  53.     spanObj.innerHTML = responseText;  
  54. }  


控制器

[html] 
  1. public function actionRepairUpload(){  
  2.         $index  = $this->request->getParam("selectedIndex");  
  3.         $pre_id = $this->request->getParam("upload_save_to_db_id");  
  4.           
  5.         $inputFileName = "repair_attached_file".$index;  
  6.         $attach = CUploadedFile::getInstanceByName($inputFileName);  
  7.           
  8.         $retValue = "";  
  9.         if($attach == null){  
  10.             $retValue = "提示:不能上传空文件。";  
  11.         }else if($attach->size > 2000000){  
  12.             $retValue = "提示:文件大小不能超过2M。";  
  13.         }else {  
  14.             $retValue = '恭喜,上传成功!';  
  15.             if($pre_id == 0){  
  16.                 $f = file_get_contents($attach->tempName);  
  17.                 $a = new Attachment();  
  18.                 $a->ref_type = "failParts";  
  19.                 $a->data = $f;  
  20.                 $a->file_path = $attach->name;  
  21.                 $a->save();  
  22.                 $cur_id = $a->id;  
  23.             }else{  
  24.                 $trans = Yii::app()->db->beginTransaction();  
  25.                 try{  
  26.                     $f = file_get_contents($attach->tempName);  
  27.                     $a = new Attachment();  
  28.                     $a->ref_type = "failParts";  
  29.                     $a->data = $f;  
  30.                     $a->file_path = $attach->name;  
  31.                     $a->save();  
  32.                     $cur_id = $a->id;  
  33.                       
  34.                     $pre = Attachment::model()->findByPk($pre_id);  
  35.                     $pre->delete();  
  36.   
  37.                     $trans->commit();  
  38.                 }catch(Exception $e){  
  39.                     $retValue = $e->getMessage();  
  40.                     $cur_id = 0;  
  41.                     $trans->rollback();  
  42.                 }  
  43.             }  
  44.             echo "<script type='text/javascript'>window.top.window.successUpload('{$retValue}',$cur_id,$index)</script>";exit();  
  45.         }  
  46.         echo "<script type='text/javascript'>window.top.window.stopUpload('{$retValue}',$index)</script>";  
  47.     }  

编程技巧