在 yii2 中使用 2 amigos 文件上传器
Using the 2 amigos file uploader in yii2
正在实现 2amigos 文件上传器,它只显示界面但不显示或上传任何文件
I have tried this:
<?= FileUploadUI::widget([
'model' => $evidence,
'attribute' => 'path',
'url' => ['media/upload', 'id' => $evidence],
'gallery' => false,
'fieldOptions' => [
'accept' => 'image/*'
],
'clientOptions' => [
'maxFileSize' => 2000000
],
// ...
'clientEvents' => [
'fileuploaddone' => 'function(e, data) {
console.log(e);
console.log(data);
}',
'fileuploadfail' => 'function(e, data) {
console.log(e);
console.log(data);
}',
],
]);
?>
嘿 Geffory 只将小部件放在视图上我们不能上传文件我们必须根据 widget.In 我的项目创建控制器我通常使用 krajee 文件输入 yii2 扩展。无论如何,我从 https://github.com/2amigos/yii2-file-upload-widget/issues/5
找到了一个示例控制器
控制器功能
public function actionUpload($id)
{
$tour = Tour::findOne($id);
if (!$tour) {
throw new NotFoundHttpException(Yii::t('app', 'Page not found'));
}
$picture = new TourPicture(['scenario' => 'upload']);
$picture->tour_id = $id;
$picture->image = UploadedFile::getInstance($picture, 'image');
if ($picture->image !== null && $picture->validate(['image'])) {
Yii::$app->response->getHeaders()->set('Vary', 'Accept');
Yii::$app->response->format = Response::FORMAT_JSON;
$response = [];
if ($picture->save(false)) {
$response['files'][] = [
'name' => $picture->image->name,
'type' => $picture->image->type,
'size' => $picture->image->size,
'url' => $picture->getImageUrl(),
'thumbnailUrl' => $picture->getImageUrl(TourPicture::SMALL_IMAGE),
'deleteUrl' => Url::to(['delete', 'id' => $picture->id]),
'deleteType' => 'POST'
];
} else {
$response[] = ['error' => Yii::t('app', 'Unable to save picture')];
}
@unlink($picture->image->tempName);
} else {
if ($picture->hasErrors(['picture'])) {
$response[] = ['error' => HtmlHelper::errors($picture)];
} else {
throw new HttpException(500, Yii::t('app', 'Could not upload file.'));
}
}
return $response;
}
您可以使用此控制器功能并根据需要添加修改您的控制器..
正在实现 2amigos 文件上传器,它只显示界面但不显示或上传任何文件
I have tried this:
<?= FileUploadUI::widget([
'model' => $evidence,
'attribute' => 'path',
'url' => ['media/upload', 'id' => $evidence],
'gallery' => false,
'fieldOptions' => [
'accept' => 'image/*'
],
'clientOptions' => [
'maxFileSize' => 2000000
],
// ...
'clientEvents' => [
'fileuploaddone' => 'function(e, data) {
console.log(e);
console.log(data);
}',
'fileuploadfail' => 'function(e, data) {
console.log(e);
console.log(data);
}',
],
]);
?>
嘿 Geffory 只将小部件放在视图上我们不能上传文件我们必须根据 widget.In 我的项目创建控制器我通常使用 krajee 文件输入 yii2 扩展。无论如何,我从 https://github.com/2amigos/yii2-file-upload-widget/issues/5
找到了一个示例控制器控制器功能
public function actionUpload($id)
{
$tour = Tour::findOne($id);
if (!$tour) {
throw new NotFoundHttpException(Yii::t('app', 'Page not found'));
}
$picture = new TourPicture(['scenario' => 'upload']);
$picture->tour_id = $id;
$picture->image = UploadedFile::getInstance($picture, 'image');
if ($picture->image !== null && $picture->validate(['image'])) {
Yii::$app->response->getHeaders()->set('Vary', 'Accept');
Yii::$app->response->format = Response::FORMAT_JSON;
$response = [];
if ($picture->save(false)) {
$response['files'][] = [
'name' => $picture->image->name,
'type' => $picture->image->type,
'size' => $picture->image->size,
'url' => $picture->getImageUrl(),
'thumbnailUrl' => $picture->getImageUrl(TourPicture::SMALL_IMAGE),
'deleteUrl' => Url::to(['delete', 'id' => $picture->id]),
'deleteType' => 'POST'
];
} else {
$response[] = ['error' => Yii::t('app', 'Unable to save picture')];
}
@unlink($picture->image->tempName);
} else {
if ($picture->hasErrors(['picture'])) {
$response[] = ['error' => HtmlHelper::errors($picture)];
} else {
throw new HttpException(500, Yii::t('app', 'Could not upload file.'));
}
}
return $response;
}
您可以使用此控制器功能并根据需要添加修改您的控制器..