Yii2 Dropzone
DropzoneJs Extention for Yii2, (*1)
A port of DropzoneJs for Yii2 Framework, (*2)
Installation
The preferred way to install this extension is through composer., (*3)
Either run, (*4)
php composer.phar require --prefer-dist boliver20/yii2-dropzone "*"
or add, (*5)
"boliver20/yii2-dropzone": "*"
to the require section of your composer.json
file., (*6)
Usage
Once the extension is installed, simply use it in your code by to create Ajax upload area :, (*7)
echo \boliver\DropZone::widget();
To pass options : (More details at dropzonejs official docs ), (*8)
echo \boliver\DropZone::widget([
'options' => [
'maxFilesize' => '2',
],
'clientEvents' => [
'complete' => "function(file){console.log(file)}",
'removedfile' => "function(file){alert(file.name + ' is removed')}"
],
]);
Example of upload method :, (*9)
public function actionUpload()
{
$fileName = 'file';
$uploadPath = './files';
$file = \yii\web\UploadedFile::getInstanceByName($fileName);
//Print file data
//print_r($file);
if ($file && $file->saveAs($uploadPath . '/' . $file->name)) {
//Now save file data to database
echo \yii\helpers\Json::encode($file);
}
return false;
}