用Yii,Mysql下载longblob文件(adobe Illustrator或png)

问题描述:

I am trying to download ai/png from mysql DB but all I get is an empty file. On the table I only have id and two longblob (file_ai, file_png). For example, file adobe illustrator view is:

echo CHtml::link('<span class="glyphicon glyphicon glyphicon-download-alt" aria-hidden="true"></span>', 
array('User/downloadFile', 'id' => $file->id, 'ext' => 'application/illustrator'), 
$htmlOptions = array('class' => 'toolMes', 'download', 'data-toggle' => 'tooltip', 'data-placement' => 'bottom', 'title' => 'Download Ai'));

In the controller:

public function actionDownloadFile($id, $ext) {
        $file = Files::model()->findByPk($id);
        if ($ext === 'application/illustrator') {
            header("Content-length:" . strlen($file->file_ai));
            header("Content-type: " . $ext . "");
            header('Content-Disposition: attachment; filename="' . $file->id . '_' . date("Y-m-d") . '.ai"');
        } else {
            header("Content-length:" . strlen($file->file_png));
            header("Content-type: image/png");
            header('Content-Disposition: attachment; filename="' . $file->id . '_' . date("Y-m-d") . '"');
        }
    }

Hope it is enough to have your precious suggestions

我正在尝试从mysql数据库下载ai / png,但我得到的只是一个空文件。 在表中 我只有id和两个longblob(file_ai,file_png)。 例如,文件adobe illustrator视图是: p>

  echo CHtml :: link('&lt; span class =“  glyphicon glyphicon glyphicon-download-alt“aria-hidden =”true“&gt;&lt; / span&gt;',
array('User / downloadFile','id'=&gt; $ file-&gt; id,'ext'=  &gt;'application / illustrator'),
 $ htmlOptions = array('class'=&gt;'toolMes','download','data-toggle'=&gt;'tooltip','data-placement'=&gt;  'bottom','title'=&gt;'下载Ai')); 
  code>  pre> 
 
 

在控制器中: p>

  public function actionDownloadFile($ id,$ ext){
 $ file = Files :: model() - &gt; findByPk($ id); 
 if($ ext ==='application / illustrator'){\  n header(“Content-length:”。strlen($ file-&gt; file_ai)); 
 header(“Content-type:”。$ ext。“”); 
 header('Content-Disposition:attachme  NT;  filename =“'。$ file-&gt; id。'_'。date(”Ymd“)。'。'”'); 
} else {
 header(“Content-length:”。strlen($ file  - &gt; file_png)); 
标题(“Content-type:image / png”); 
标题('Content-Disposition:attachment; filename =“'。$ file-&gt; id。'_'。date  (“Ymd”)。'''); 
} 
} 
  code>  pre> 
 
 

希望能够得到您宝贵的建议 p>

Your not echoing out the actual file contents!

If you are storing images in your database you need to do the echo after you throw the headers...

echo $file->file_ai;