获得
通过filemanager上传图片时,来自ajax请求的错误。在null
上调用成员函数saveAs()
配置:
...
'modules' => [
'site' => [
'class' => 'app\modules\site\Module',
],
'user' => [
'class' => 'app\modules\user\Module',
'controllerNamespace' => 'app\modules\user\controllers\frontend',
'viewPath' => '@app/modules/user/views/frontend',
],
'admin' => [
'class' => 'app\modules\admin\Module',
'layout' => '@app/views/layouts/admin',
'modules' => [
'user' => [
'class' => 'app\modules\user\Module',
'controllerNamespace' => 'app\modules\user\controllers\backend',
'viewPath' => '@app/modules/user/views/backend',
],
'pages' => [
'class' => 'bupy7\pages\Module',
'controllerNamespace' => 'bupy7\pages\controllers\backend',
'controllerMap' => [
'manager' => [
'class' => 'bupy7\pages\controllers\backend\ManagerController',
],
],
],
'gallery' => [
'class' => 'sadovojav\gallery\Module',
'basePath' => '@webroot/galleries',
],
'filemanager' => [
'class' => 'pendalf89\filemanager\Module',
// Upload routes
'routes' => [
// Base absolute path to web directory
'baseUrl' => '',
// Base web directory url
'basePath' => '@webroot',
// Path for uploaded files in web directory
'uploadPath' => 'uploads',
],
// Thumbnails info
'thumbs' => [
'small' => [
'name' => 'Small',
'size' => [100, 100],
],
'medium' => [
'name' => 'Medium',
'size' => [300, 200],
],
'large' => [
'name' => 'Big',
'size' => [500, 400],
],
],
],
],
],
...
Controller中的actionUpload()
public function actionUpload()
{
$model = new Mediafile();
$routes = $this->module->routes;
$rename = $this->module->rename;
$model->saveUploadedFile($routes, $rename);
Yii::$app->response->format = Response::FORMAT_JSON;
$tagIds = Yii::$app->request->post('tagIds');
if ($tagIds !== 'undefined') {
$model->setTagIds(explode(',', $tagIds));
}
$bundle = FilemanagerAsset::register($this->view);
if ($model->isImage()) {
$model->createThumbs($routes, $this->module->thumbs);
}
$response['files'][] = [
'url' => $model->url,
'thumbnailUrl' => $model->getDefaultThumbUrl($bundle->baseUrl),
'name' => $model->filename,
'type' => $model->type,
'size' => $model->file->size,
'deleteUrl' => Url::to(['file/delete', 'id' => $model->id]),
'deleteType' => 'POST',
];
return $response;
}
检查模型,
saveUploadedFile()中的$ this-> file = UploadedFile :: getInstance($ this,'file');
返回null而不是object。
问题是执行了saveUploadedFile()函数,保存了文件并在数据库中创建了记录,但它返回错误。
答案 0 :(得分:0)
UploadedFile :: getInstance返回文件数组。使用getInstanceByName或UploadedFile :: getInstance($ model,'file')[0]。