troy / yii2-image-upload
upload image via ajax
Installs: 2 846
Dependents: 0
Suggesters: 0
Security: 0
Stars: 10
Watchers: 3
Forks: 4
Open Issues: 1
Language:JavaScript
Type:yii2-extension
Requires
- yiisoft/yii2: *
This package is not auto-updated.
Last update: 2025-04-22 09:36:23 UTC
README
upload image via ajax
Installation
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist troy/yii2-image-upload ""
or add
"troy/yii2-image-upload": ""
to the require section of your composer.json
file.
Usage
Once the extension is installed, simply use it in your code by :
<a id="imageUpload" href="javascript:;">上传图片</a>
<?= \troy\ImageUpload\ImageUpload::widget( [ 'targetId' => 'imageUpload',//html dom id 'config' =>[ 'action' =>Yii::$app->getUrlManager()->createUrl(['site/index']) ] ] ); ?>
You also can add some events for it such as onComplete function
<?= ..... use yii\web\JsExpression; ..... \troy\ImageUpload\ImageUpload::widget( [ 'targetId' => 'imageUpload',//html dom id 'config' =>[ 'action' =>Yii::$app->getUrlManager()->createUrl(['site/index']), 'onComplete' => new JsExpression("function(fileName, responseJSON){ something todo...... }") ] ] ); ?>
if you want to the the UploadAction in this ext you can use :
class SiteController extends Controller
{
public function actions()
{
return [
'upload' => [
'class' => 'troy\ImageUpload\UploadAction',
'successCallback' => [$this, 'successCallback'],
'beforeStoreCallback' => [$this,'beforeStoreCallback']
],
]
}
public function successCallback($store,$file)
{
}
public function beforeStoreCallback($file)
{
}
}