使用 yii 框架从数据库 BLOB 下载链接 [英] Download link from Database BLOB with yii Framework

查看:24
本文介绍了使用 yii 框架从数据库 BLOB 下载链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Yii 框架完全陌生,我不明白如何创建带有弹出窗口的下载链接,询问您是否要保存文件.

I'm totaly new to the Yii Framework and i don't understand the how to create a download link with a popup window that asks if you whant to save the file.

到目前为止我所知道和拥有的:

What i know and have till now:

  1. 我知道 CHtml::link(...) 是如何工作的并且已经实现了它.
  2. 我从数据库中提取了 Blob 内容并将其保存在 $data->file 中
  3. 我想点击的链接应该在 CListView 内

我的数据库 USERZ 看起来像这样

My database USERZ looks like this

+---------+-------------+------+-----+---------+----------------+
| Field   | Type        | Null | Key | Default | Extra          |
+---------+-------------+------+-----+---------+----------------+
| id      | int(11)     | NO   | PRI | NULL    | auto_increment |
| file    | blob        | NO   |     | NULL    |                |
| USER_id | int(11)     | NO   | MUL | NULL    |                |
| zname   | varchar(40) | NO   |     | NULL    |                |
| date    | date        | NO   |     | NULL    |                |
+---------+-------------+------+-----+---------+----------------+

我的控制器功能(SiteController.php)

my Controller funktion (SiteController.php)

public function actionVerwal()
{
    $model2=new USERZ;
    $this->render('verwal',array('model2'=>$model2));

}

和视图 (verwal.php)

and the view (verwal.php)

<?php
 $this->widget('zii.widgets.CListView', array(
    'dataProvider'=>$model2->a(Yii::app()->user->getId()),
    'itemView'=>'_verwal',
));

//function a($id) 只是确保提供正确的用户数据

//function a($id) just makes sure that the right user data is provided

我不明白的部分(_verwal.php):

The Part that i dont understand (_verwal.php):

<b><?php echo CHtml::encode($data->getAttributeLabel('Files')); ?>:</b>
<?php echo CHtml::link(CHtml::encode($data->zname), $data->file); ?>
<br />

我怎样才能将文件名变成一个链接以便它提供下载.我拥有所有需要的文件内容和名称,但我不知道如何将它们放在一起并使它们成为 1 个下载文件弹出窗口.

How can i make the File name into a Link so it provides a download. I have all it needs the Content of the File and the Name but i dont know how to put them together and make them into 1 download file popup.

推荐答案

文件下载由对您的应用程序的单独请求进行处理,该请求会导致文件头和正在发送信息.

File downloads are handled by a separate request to your application that results in the file headers & info being sent.

所以你想要做的是在你的控制器中创建一个名为public function actionDownload($id)的新动作.然后在您的视图中,您将链接到该操作.

So what you want to do is it to create a new action in your controller called something like public function actionDownload($id). Then in your view you will link to that action.

<?php echo CHtml::link(CHtml::encode($data->zname), array('site/download', 'id' => $data->id)); ?>

我们发送数据的 id,以便操作知道它正在使用什么模型.

We send the id of the data so the action knows what model it's working with.

$user = USERZ::model()->findByPk($id);

现在您可以通过在下载操作中发送正确的标头来将用户文件发送给他们.

Now you can send the users file to them by sending the correct headers within your download action.

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=MY_FILE_NAME');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . count($user->file));
ob_start();
echo $user->file;
exit;

不过,您不应该真正将文件存储在数据库中,请考虑将它们存储在诸如 protected/files 之类的文件夹中.

You shouldn't really be storing your files in the database though, consider storing them in a folder like protected/files.

这篇关于使用 yii 框架从数据库 BLOB 下载链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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