huangdingbo/yii2-uploader
Composer 安装命令:
composer require huangdingbo/yii2-uploader
包简介
Yii2 大文件上传(支持断点续传)
README 文档
README
1、安装
composer: composer require huangdingbo/yii2-uploader
git: https://github.com/huangdingbo/yii2-uploader
2、使用
1、创建数据表
将 sys_upload_files.sql 导入数据库中
2、配置组件
在main.php的components中加入:
'uploader' => [ 'class' => 'uploader\FileUploaderComponent', 'allowExt' => '*', //运行的文件扩展名, "*" || ["*"] "" ["png","jpg"], * 表示不限制 'basePath' => '@webroot/static/uploader', //上传文件的根路径 'temp_path' => '@webroot/static/temp', //临时文件的存放路径 'chunk_path' => '@webroot/static/chunks', //切片的临时存放路径 'web_path' => '@web/static/uploader', //文件上传后的访问路径 'db' => 'db', //数据库连接组件 'table' => 'sys_upload_files' //数据表 ]
3、实例
后端DEMO:
$fileInfo = \Yii::$app->request->post(); $fileName = ArrayHelper::getValue($fileInfo,'fileName'); $size = ArrayHelper::getValue($fileInfo,'size'); $type = ArrayHelper::getValue($fileInfo,'type'); $chunkNum = ArrayHelper::getValue($fileInfo,'chunkNum'); $chunk = ArrayHelper::getValue($fileInfo,'chunk'); $chunkSize = ArrayHelper::getValue($fileInfo,'chunkSize'); if(!$fileName || !$size || !$type || !$chunkNum || !$chunk || !$chunkSize || !$_FILES["file"]["tmp_name"]){ return ["ok" => false,"code" => 201,"msg" => "参数不完整"]; } //实例化组件 $handler = \Yii::$app->uploader; //设置参数 $handler->setFileName($fileName)->setSize($size)->setType($type)->setTotalChunk($chunkNum)->setCurrentChunk($chunk)->setChunkSize($chunkSize); //执行 $result = $handler->execute(); if (is_array($result)){ return $result; }else{ return $handler->getError()[0]; }
前端DEMO:
</template> <input type="file" value="断点续传" @change="getFileDemo($event)"> </template> <script> export default { data(){ return { chunkSize: 1024 * 1024, //切片的大小 data:[], //文件信息 currentTrunk:0, //当前上传分片的索引 index:0, //服务器存在分片的索引 } }, methods:{ sendFileDemo(){ let start = this.currentTrunk * this.chunkSize; let end = 0; if(this.currentTrunk+1 === this.data.chunkNum){ end = this.data.len }else { end = start + this.chunkSize; } let blob = this.data.file.slice(start,end); let formData = new FormData(); formData.append('file', blob); formData.append('fileName', this.data.name); formData.append('size', this.data.len); formData.append('type', this.data.type); formData.append('chunkNum', this.data.chunkNum); formData.append('chunk', (this.currentTrunk+1)); formData.append('chunkSize', this.chunkSize); let config = { headers: { 'Content-Type': 'multipart/form-data', } }; this.axios.post("http://192.168.1.30:9527/CYApi/test/test/index",formData,config).then(res=>{ if (res.ok){ if (this.currentTrunk+1 === this.data.chunkNum){ console.log("上传完成:",res) }else { this.currentTrunk++; this.sendFileDemo(); console.log(res) } } console.log(this.currentTrunk) }) }, getFileDemo(event){ let _file = event.target.files[0]; this.data = { "file" : event.target.files[0], "len" : _file.size, "name" : _file.name, "type" : _file.type, "chunkNum" : Math.ceil(_file.size/this.chunkSize) }; console.log(this.data); this.sendFileDemo(); }, } } </script>
返回状态码说明
[
200 => ["code" => 200,"msg" => "上传成功"],
1000 => ["code" => 1000,"msg" => "文件名不能为空"],
1001 => ["code" => 1001,"msg" => "文件缺少扩展名"],
1002 => ["code" => 1002,"msg" => "不允许上传该类型文件"],
1003 => ["code" => 1003,"msg" => "文件大小为空"],
1004 => ["code" => 1004,"msg" => "分片大小不能为空"],
1005 => ["code" => 1005,"msg" => "分片信息不能为空"],
1006 => ["code" => 1006,"msg" => "当前分片大于了总分片数据,文件修改了?"],
1007 => ["code" => 1007,"msg" => "临时文件不存在"],
1008 => ["code" => 1008,"msg" => "未配置运行的文件扩展名"],
1009 => ["code" => 1009,"msg" => "allowExt、temp_path、hunk_path、basePath需定义"],
1010 => ["code" => 1010,"msg" => "存在同名文件"],
1111 => ["code" => 1111,"msg" => "服务器已经存在分片","index" => 0],
2222 => ["code" => 2222,"msg" => "切片上传完成"],
];
huangdingbo/yii2-uploader 适用场景与选型建议
huangdingbo/yii2-uploader 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 49 次下载、GitHub Stars 达 2, 最近一次更新时间为 2020 年 09 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「upload」 「extension」 「component」 「widget」 「yii2」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 huangdingbo/yii2-uploader 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 huangdingbo/yii2-uploader 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 huangdingbo/yii2-uploader 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
FSi DataSource Component
A custom URL rule class for Yii 2 which allows to create translated URL rules
The file manager intended for using Laravel with CKEditor / TinyMCE / Colorbox
The Yii2 extension uses jQuery jquery.carousel-1.1.min.js and makes image carousel from php array of structure defined.
TYPO3 CMS extension to create gallery content element with preset crop ratios and pagination
UI Kit 3 Extension for Yii2
统计信息
- 总下载量: 49
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-09-04