Yii CUploadedFile带验证的多文件上传
Yii CUploadedFile带验证的多文件上传
JS
[html]
var filecount=1;
// 新增一个上传文件控件
function addfile(){
var filediv = document.getElementById("upForms");
var fileitemdiv = document.createElement("form");
filecount++;
var content = "
filecount + "> filecount + ");'>删除 filecount + " style='color:red;'>
filecount + ");'>删除
filecount + " style='color:red;'>
filecount + " name=selectedIndex />
filecount + " value=0 />";
fileitemdiv.id = "fileitemdiv"+filecount;
fileitemdiv.method = "post";
fileitemdiv.enctype = "multipart/form-data";
fileitemdiv.target = "upload_target";
fileitemdiv.action = "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 "";exit();
}
echo "";
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~