Yii2 显示存储在 BLOB 数据库字段中的图像 [英] Yii2 Display image stored in BLOB database field

查看:22
本文介绍了Yii2 显示存储在 BLOB 数据库字段中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候

我需要在 Yii2 的 LISTVIEW 或类似小部件中显示存储在 BLOB 数据库字段中的图像

我有 actionCreate 将图像保存在名为 honra 的表中作为 BLOB

public function actionCreate()
{
    $model = new Honra();

    if ($model->load(Yii::$app->request->post()) && $model->save()) {
        $file = UploadedFile::getInstance($model,'binaryfile');
        $model->binaryfile=$file;
        return $this->redirect(['view', 'id' => $model->id]);
    } else {
        return $this->render('create', [
            'model' => $model,
        ]);
    }
}

我也有一个名叫 Honra 的模型

class Honra extends yiidbActiveRecord{

public static function tableName()
{
    return 'honra';
}

/**
 * @inheritdoc
 */
public function rules()
{
    return [
        [['nome', 'texto'], 'required'],
        [['texto', 'binaryfile'], 'string'],
        [['ativo'], 'integer'],
        [['nome'], 'string', 'max' => 255],
        [['fileName'], 'string', 'max' => 100],
        [['fileType'], 'string', 'max' => 50]
    ];
}

/**
 * @inheritdoc
 */
public function attributeLabels()
{
    return [
        'id' => Yii::t('app', 'ID'),
        'nome' => Yii::t('app', 'Nome'),
        'texto' => Yii::t('app', 'Texto'),
        'fileName' => Yii::t('app', 'File Name'),
        'fileType' => Yii::t('app', 'File Type'),
        'binaryfile' => Yii::t('app', 'Binaryfile'),
        'ativo' => Yii::t('app', 'Ativo'),
    ];
}

}

图片成功存入名为binaryfile的表字段

我需要在 LISTVIEW 或类似小部件中名为 view2 的视图中显示存储在数据库中的每个 blob 图像

谁知道我需要在 view2 和 HonraController 中放入什么代码块来实现这一点??

编辑

已解决

这是解决问题的代码,现在一切正常

public function actionCreate()
{
    $model = new Honra();

     if (Yii::$app->request->isPost) {

            $model->file = UploadedFile::getInstance($model, 'file');
            $filePath = 'uploadsimg/'  . $model->file->baseName . '.' . $model->file->extension;
            $model->fileName = $filePath;
            $model->save();

            $model->file->saveAs('uploadsimg/' . $model->file->baseName . '.' . $model->file->extension);
            if ($model->load(Yii::$app->request->post()) && $model->save()) {
                return $this->redirect(Url::toRoute(['create']));
            }
        }
        return $this->render('create', ['model' => $model]);
}

非常感谢 MIHAI 的帮助

推荐答案

这里是 Yii2 的官方文档 https://github.com/yiisoft/yii2/blob/master/docs/guide/input-file-upload.md这很简单.

Here is the official documentation on Yii2 https://github.com/yiisoft/yii2/blob/master/docs/guide/input-file-upload.md It is quite simple.

一些评论:
1) 照片可能是私人的,如果有人猜测其他人的路径,这可能是一个大问题.在这种情况下,您可以将文件放在不在 web 文件夹中的某个位置.如果您这样做,那么您必须使用资产向它们显示 assetManager->getPublishedUrl('@app/folder')?>/fav.ico
2) 如果图片不是那么敏感,那么您可以将它们保存在网络文件夹中的某个位置.

some comments:
1) the pics might be private and it might be a big problem if somebody guesses the path of others. In this case you can put the file someplace NOT in the web folder. If you do this then you have to use the assets to show them assetManager->getPublishedUrl('@app/folder')?>/fav.ico
2) if the pics are not so sensitive then you can just save them someplace in the web folder.

如您所见,您可能会在上传相同文件名 2 次时遇到问题,因为 1 次会覆盖另一个.你总是可以重命名文件,我实际上会把他们的代码改成这样

As you can see you might have problems with uploading the same file name 2 times, as 1 will override the other. You can always rename the file, I would actually change their code to something like this

        if ($model->file && $model->validate()) {
$model->file = UploadedFile::getInstance($model, 'file');
            $filePath = 'uploads/' . hash('ripemd160', microtime()) . $model->file->baseName . '.' . $model->file->extension;
            $model->fileName = $filePath;
            $model->save();

            $model->file->saveAs('uploadsimg/' . $model->file->baseName . '.' . $model->file->extension);
            if ($model->load(Yii::$app->request->post()) && $model->save()) {
                return $this->redirect(Url::toRoute(['create']));
            }
        }

这篇关于Yii2 显示存储在 BLOB 数据库字段中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆