request->get($this->uploadQueryParam)) { $this->uploadParam = Yii::$app->request->get($this->uploadQueryParam); } if ($this->uploadOnlyImage !== true) { $this->_validator = 'file'; } } /** * @inheritdoc */ public function run() { Yii::$app->response->format = Response::FORMAT_JSON; if (Yii::$app->request->isPost) { $files = UploadedFile::getInstancesByName($this->uploadParam); // p($files); if (!$this->multiple) { $res = [$this->uploadOne($files[0])]; } else { $res = $this->uploadMore($files); } $result = [ 'files' => $res ]; if ($this->callback instanceof \Closure) { $result = call_user_func($this->callback, $result); } return $result; } else { throw new BadRequestHttpException('Only POST is allowed'); } } private function uploadMore(array $files) { $res = []; foreach ($files as $file) { $result = $this->uploadOne($file); $res[] = $result; } return $res; } private function uploadOne(UploadedFile $file) { try { $model = new DynamicModel(compact('file')); $model->addRule('file', $this->_validator, $this->validatorOptions)->validate(); if ($model->hasErrors()) { throw new Exception($model->getFirstError('file')); } else { $attachment = Attachment::uploadFromPost($this->path, $file); $result = [ 'id' => $attachment->id, 'name' => $attachment->name, 'hash' => $attachment->hash, 'url' => $attachment->url, 'path' => $attachment->path, 'extension' => $attachment->extension, 'type' => $attachment->type, 'size' => $attachment->size ]; if ($this->uploadOnlyImage !== true) { $result['filename'] = $file->name; } } if ($this->itemCallback instanceof \Closure) { $result = call_user_func($this->itemCallback, $result); } } catch (Exception $e) { $result = [ 'error' => $e->getMessage() ]; } return $result; } }