Yii CUploadedFile带验证的多文件上传(三)

作者:zccst

注:上传文件是存放在数据库的一张表中。如果是存放在某一个文件路径下,则使用saveAs即可。

一、前端

<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>



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>";
}



如果您觉得本文的内容对您的学习有所帮助,您可以微信:
[img]http://dl2.iteye.com/upload/attachment/0109/0668/fb266dfa-95ca-3d09-b41e-5f04a19ba9a1.png[/img]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一段 Yii2 上传 Excel 文件并保存的代码: // 在控制器中处理上传文件的操作 public function actionUploadExcel() { $model = new ExcelModel(); if (Yii::$app->request->isPost) { $model->excelFile = UploadedFile::getInstance($model, 'excelFile'); if ($model->upload()) { // 上传成功,保存文件并处理数据 $filePath = $model->getFilePath(); $excelData = $this->processExcelData($filePath); // 保存数据到数据库 $this->saveExcelData($excelData); return $this->redirect(['index']); } } return $this->render('uploadExcel', ['model' => $model]); } // ExcelModel 模型类 class ExcelModel extends \yii\base\Model { public $excelFile; public function rules() { return [ [['excelFile'], 'file', 'extensions' => 'xls, xlsx'], ]; } public function upload() { if ($this->validate()) { $this->excelFile->saveAs($this->getFilePath()); return true; } else { return false; } } public function getFilePath() { return Yii::getAlias('@webroot') . '/uploads/' . $this->excelFile->baseName . '.' . $this->excelFile->extension; } } // 处理 Excel 数据的方法 private function processExcelData($filePath) { // 使用 PHPExcel 库读取 Excel 文件 $objPHPExcel = \PHPExcel_IOFactory::load($filePath); $sheetData = $objPHPExcel->getActiveSheet()->toArray(null, true, true, true); // 处理数据 $excelData = []; foreach ($sheetData as $row) { $rowData = []; foreach ($row as $cell) { $rowData[] = $cell; } $excelData[] = $rowData; } return $excelData; } // 保存 Excel 数据到数据库的方法 private function saveExcelData($excelData) { // 将数据保存到数据库 // ... } 希望这段代码能够帮助到你!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值