首页 文章

Cakephp - MeioUpload - 如何下载上传的文件

提问于
浏览
0

我刚刚开始使用meioupload从cakephp上传我的文件,但是,我不知道如何编写下载代码 .

我尝试使用以下代码:

echo $file['Image']['dir'].$file['Image']['filename'];

但是,似乎代码是错误的 . 因为它输出以下内容:

uploads\image\filenamedb_fyp_2.txt

我如何下载文件db_fyp2.txt?

3 回答

  • 0

    好吧,你告诉你的应用程序就是回显文件路径和's exactly what it' . 如果您只想在浏览器中打开文件,请使用 redirect 控制流,例如:

    class YourController extends AppController {
        public function upload() {
            // You upload logic here, followed by ...
            $this->redirect($file['Image']['dir'].$file['Image']['filename']);
        }
    }
    

    如果您希望浏览器提供下载文件,请发送相应的Content-Disposition header(参见示例#1) .

    header('Content-Disposition: attachment; filename="' . $file['Image']['filename'] . '"');
    readfile($file['Image']['dir'].$file['Image']['filename']);
    
  • 0

    尝试将你的 root 添加到你要打印的地方 . 只有它给出了一个网址它才会起作用 . 你在这里做的主要做法是你必须把它作为一个链接而不是 just printing it.

    $this->Html->link('Link Name',$path to that file);
    
  • 0

    谢谢你的回答,但我已经找到了如何使用cakephp的mediaview下载文件 .

    无论如何,要回答我自己的问题,要下载具有各种扩展名的文件,可以使用以下代码 .

    使用cakephp的Media View - 在控制器中

    public function download($id){
    
    
    
         $this->viewClass = 'Media';
         $this->autoLayout = false;
    
    
        $file = $this->Image->findById($id); 
         $link = $file['Image']['dir'];
    
    
    
         $params = array(
         'id' => $file['Image']['filename'],
         'name' => $file['Image']['filename'],
         'download' => true,
         'mimeType' => $file['Image']['mimetype'], 
         'extension' => array('pdf','zip','txt'),
         'path' => $link.DS
         );
    
        $this->set($params);
    

相关问题