/**
  * @inheritdoc
  */
 public function run()
 {
     if (Yii::$app->request->isPost) {
         $file = UploadedFile::getInstanceByName($this->paramName);
         $model = new DynamicModel(compact('file'));
         $model->addRule('file', $this->_validator, $this->validatorOptions)->validate();
         if ($model->hasErrors()) {
             $result = ['error' => $model->getFirstError('file')];
         } else {
             if ($this->unique === true && $model->file->extension) {
                 $model->file->name = uniqid() . '.' . $model->file->extension;
             }
             if ($model->file->saveAs($this->path . $model->file->name)) {
                 $result = ['name' => $model->file->name];
             } else {
                 $result = ['error' => Widget::t('fileapi', 'ERROR_CAN_NOT_UPLOAD_FILE')];
             }
         }
         Yii::$app->response->format = Response::FORMAT_JSON;
         return $result;
     } else {
         throw new BadRequestHttpException('Only POST is allowed');
     }
 }
Exemple #2
0
<?php 
if ($crop === true) {
    ?>
    <div id="modal-crop" class="modal fade" tabindex="-1" role="dialog" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel"><?php 
    echo Widget::t('fileapi', 'MODAL_TITLE');
    ?>
</h4>
                </div>
                <div class="modal-body">
                    <div id="modal-preview"></div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal"><?php 
    echo Widget::t('fileapi', 'MODAL_CANCEL');
    ?>
</button>
                    <button type="button" class="btn btn-primary crop"><?php 
    echo Widget::t('fileapi', 'MODAL_SAVE');
    ?>
</button>
                </div>
            </div>
        </div>
    </div>
<?php 
}