troy/yii2-image-upload

upload image via ajax

Installs: 2 822

Dependents: 0

Suggesters: 0

Security: 0

Stars: 10

Watchers: 3

Forks: 4

Open Issues: 1

Language:JavaScript

Type:yii2-extension

1.0 2014-12-17 03:01 UTC

This package is not auto-updated.

Last update: 2024-09-24 07:01:18 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)
      {
      }
  }