2016-11-24 115 views
0

我跟着Cakes Cookbook发送文件(http://book.cakephp.org/3.0/en/controllers/request-response.html#sending-files),但我一直面临一个奇怪的问题。 PDF,DOC和其他二进制文件工作得很好。但是,当我尝试下载/显示图像(JPG或PNG)时,文件会自行破坏。CakePHP 3的图像下载损坏

下载的文件不被识别为图像。它的尺寸与原来的尺寸完全相同,但是当我比较它们时,它们完全不同。

我在网上找不到任何类似蛋糕的东西,所以我希望你能帮助我!

下面的代码是我的下载操作

public function arquivo($id) { 
    $file = $this->Arquivos->get($id); 

    $this->response->file($file['filename'], ['download' => true]); 
    // Return response object to prevent controller from trying to render 
    // a view. 

    return $this->response; 
} 

响应头:

接受-范围:字节

的Cache-Control:无店铺,无缓存,必须重新验证,后检查= 0,预检= 0

连接:保持活跃

的Content-Length:121000

内容类型:图像/ JPEG

日期:星期四,2016年11月24日16时17分49秒GMT

到期日:星期四,1981年11月19日08: 52:00 GMT

保持活动:超时= 5,最大= 100

杂注:无缓存

服务器:Apache/2.4.10(Ubuntu)

+0

检查无误MIME TYPES你有没有检查'$文件[”文件名']'是你的文件的完整路径? –

+0

是的,我登录了它,它是正确的。而且,它确实发送了一个具有相同大小和名称的文件。而且,正如我所说,pdf文件可以工作 –

+0

您应该调试图像的文件MIME类型。 –

回答

0

检查您的文件路径和MIME类型是否正确。

public function arquivo($id) { 
    $file = $this->Arquivos->get($id); 
    $this->response->file(
     $file['filename'], #Check $file['filename'] is full path of your download file 
     [ 
      'download' => true, 
      'name' => 'Your_Download_File_Name_Here' 
     ] 
    ); 
    return $this->response; 
} 

例子:

$file['filename']变量应该是/path/to/your/file.jpg

同样来自CakePHP的3 Sending or Downloading Files