您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

yii2怎么用uploadify

2025/5/19 14:18:36发布14次查看
uploadify是来自国外的一款优秀jquery插件,主要功能是批量上传文件
下载uploadify控件可以到git网站上找,找到自己适合用的.例如我们下面用个就是从
https://github.com/xjflyttp/yii2-uploadify-widget 下载的,里面也有详细的操作文档。   (推荐学习:yii教程)
① 配置composer.json (就存在网站根目录下),找到require,然后添加我们的uploadify
"require": {"php": ">=5.4.0","yiisoft/yii2": ">=2.0.6","yiisoft/yii2-bootstrap": "*","yiisoft/yii2-swiftmailer": "*","xj/yii2-uploadify-widget": "~2.0.0" //这里是新添加的},
② 利用composer 将yii2-uploadify-widget 下载到我们网站, 打开在网站根目录运行命令行 ,等待下载如图:
③ 视图的处理
<?phpuse yii\web\jsexpression;//外部tagecho html::fileinput('test', null, ['id' => 'test']);echo uploadify::widget([ 'url' => yii\helpers\url::to(['s-upload']), 'id' => 'test', //需要跟上fileinput 的id 对应 'csrf' => true, 'rendertag' => false, 'jsoptions' => [ 'width' => 100, 'height' => 40, 'onuploaderror' => new jsexpression(<<<eof function(file, errorcode, errormsg, errorstring) { console.log('the file ' + file.name + ' could not be uploaded: ' + errorstring + errorcode + errormsg); }eof ), 'onuploadsuccess' => new jsexpression(<<<eof function(file, data, response) { data = json.parse(data); if (data.error) { console.log(data.msg); } else { //在此处理你的图片 console.log(data.fileurl); } }eof
④ 配置controller 层
<?phpnamespace \backend\controllers;use xj\uploadify\uploadaction;class testcontroller extends \yii\web\controller{ public function actions() { return [ 's-upload' => [ 'class' => uploadaction::classname(), //磁盘目录 'basepath' => '@webroot/upload', //访问目录 'baseurl' => '@web/upload', //防止跨站攻击 'enablecsrf' => true, // default 'postfieldname' => 'filedata', // default //begin method 'format' => [$this, 'methodname'], //end method //begin closure by-hash //是否覆盖相同文件 'overwriteifexist' => true, //创建图片名称 'format' => function (uploadaction $action) { $fileext = $action->uploadfile->getextension(); $filename = sha1_file($action->uploadfile->tempname); return "{$filename}.{$fileext}"; }, //想要多调用这个函数 'format' => function (uploadaction $action) { $fileext = $action->uploadfile->getextension(); $filehash = sha1(uniqid() . time()); $p1 = substr($filehash, 0, 2); $p2 = substr($filehash, 2, 2); return "{$p1}/{$p2}/{$filehash}.{$fileext}"; }, */ 'validateoptions' => [ 'extensions' => ['jpg', 'png'], //后缀验证 'maxsize' => 1 * 1024 * 1024, //上传大小限制 ], //验证前处理 'beforevalidate' => function (uploadaction $action) { //throw new exception('test error'); }, //验证后处理 'aftervalidate' => function (uploadaction $action) {}, //保存前处理 'beforesave' => function (uploadaction $action) {}, //保存后处理 'aftersave' => function (uploadaction $action) { //$action->output 返回给浏览器的数据 //返回url 图片返回的路径 //$action->getweburl() //图片的名称 //$action->getfilename(); //保存图片的物理位置 //$action->getsavepath(); //输出 //$action->output['fileurl'] = $action->getweburl(); //$action->output['filename'] = $action->getfilename(); //$action->output['filepath'] = $action->getsavepath(); //$action->getfilename(); // "image/yyyymmddtimerand.jpg" //$action->getweburl(); // "baseurl + filename, /upload/image/yyyymmddtimerand.jpg" //$action->getsavepath(); // "/var/www/htdocs/upload/image/yyyymmddtimerand.jpg" $action->output[‘fileurl’] = $action->getweburl() }, ], ]; } }?>
以上就是yii2怎么用uploadify的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product