Yii CUploadedFile带验证的多文件上传

【字号: 日期:2022-06-12浏览:34作者:雯心
<div id='upForms'> <form id='fileitemdiv1' action='<?php echo $this->createUrl(’repairUpload’); ?>' method='post' enctype='multipart/form-data' target='upload_target'> <input type='file' name='repair_attached_file1' /> <input type='submit' name='submitBtn' value=’立即上传’ /> <span id='upload_repairinfo_success1' style='color:red;'></span> <input type='hidden' name='selectedIndex' value='1' /> <!-- 记录上传成功后的id --> <input type='hidden' name='upload_save_to_db_id' id='upload_save_to_db_id1' value='0' /> </form> <iframe id='upload_target' name='upload_target' src='https://www.6hehe.com/bcjs/8165.html#' style='width:0;height:0;border:0px solid #fff;'></iframe> </div> <div> <input type='button' value='增加附件' onclick='addfile();'> <input type='hidden' id='up_success_file_ids' /> </div>

JS

[html]var filecount=1; // 新增一个上传文件控件 function addfile(){ var filediv = document.getElementById('upForms'); var fileitemdiv = document.createElement('form'); filecount++; var content = '<input type=file name=repair_attached_file'+ filecount + '> <input type=submit name=submitBtn value=’立即上传’ /> <a href=’javascript:removefile('+ filecount + ');’>删除</a> <span id=upload_repairinfo_success'+ filecount + ' style=’color:red;’></span><input type=hidden value='+ filecount + ' name=selectedIndex /> <input type=hidden name=upload_save_to_db_id id=upload_save_to_db_id'+ filecount + ' value=0 />'; fileitemdiv.id = 'fileitemdiv'+filecount; fileitemdiv.method = 'post'; fileitemdiv.enctype = 'multipart/form-data'; fileitemdiv.target = 'upload_target'; fileitemdiv.action = '<?php echo $this->createUrl(’repairUpload’); ?>'; fileitemdiv.innerHTML = content; filediv.appendChild(fileitemdiv); } //删除指定上传文件控件 function removefile(fileIndex){ var filediv = document.getElementById('upForms'); var fileitemdiv = document.getElementById('fileitemdiv'+fileIndex); filediv.removeChild(fileitemdiv); } //回调成功 function successUpload(responseText,id,fileIndex){ // 1,获取值 var ids = document.getElementById('up_success_file_ids').value; if(ids){ document.getElementById('up_success_file_ids').value = ids+’,’+id; }else{ document.getElementById('up_success_file_ids').value = id; } // 2,本次上传成功,则覆盖之前上传成功的文件 document.getElementById('upload_save_to_db_id'+fileIndex).value = id; // 3,提示上传成功 var spanObj = document.getElementById('upload_repairinfo_success'+fileIndex); //spanObj.innerHTML = '上传成功'; spanObj.innerHTML = responseText; } //回调失败 function stopUpload(responseText,fileIndex){ // 提示 var spanObj = document.getElementById('upload_repairinfo_success'+fileIndex); spanObj.innerHTML = responseText; }

控制器

[html] public function actionRepairUpload(){ $index = $this->request->getParam('selectedIndex'); $pre_id = $this->request->getParam('upload_save_to_db_id'); $inputFileName = 'repair_attached_file'.$index; $attach = CUploadedFile::getInstanceByName($inputFileName); $retValue = ''; if($attach == null){ $retValue = '提示:不能上传空文件。'; }else if($attach->size > 2000000){ $retValue = '提示:文件大小不能超过2M。'; }else { $retValue = ’恭喜,上传成功!’; if($pre_id == 0){ $f = file_get_contents($attach->tempName); $a = new Attachment(); $a->ref_type = 'failParts'; $a->data = $f; $a->file_path = $attach->name; $a->save(); $cur_id = $a->id; }else{ $trans = Yii::app()->db->beginTransaction(); try{ $f = file_get_contents($attach->tempName); $a = new Attachment(); $a->ref_type = 'failParts'; $a->data = $f; $a->file_path = $attach->name; $a->save(); $cur_id = $a->id; $pre = Attachment::model()->findByPk($pre_id); $pre->delete(); $trans->commit(); }catch(Exception $e){ $retValue = $e->getMessage(); $cur_id = 0; $trans->rollback(); } } echo '<script type=’text/javascript’>window.top.window.successUpload(’{$retValue}’,$cur_id,$index)</script>';exit(); } echo '<script type=’text/javascript’>window.top.window.stopUpload(’{$retValue}’,$index)</script>'; }
相关文章: