Yii CUploadedFile带验证的多文件上传
2018-07-13 08:46:45来源:编程学习网 阅读 ()
- <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="#" 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
- 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;
- }
控制器
- 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>";
- }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- laravel5.2表单验证,并显示错误信息的实例 2019-09-30
- php伪静态验证码不显示的解决方案 2019-09-30
- php开发中yii中新增用户验证怎么实现 2019-09-17
- PHP开发api接口安全验证的实例,值得一看 2019-08-23
- PHP基于Token的身份验证的方法,可参考学习下 2019-08-23
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash