2014-02-10 45 views

回答

1

我的版本是这样的:

/** 
* @Route("/download/{file_id}", name="download_file") 
*/ 
public function downloadAction($file_id) 
{ 
    // get your filepath from db somehow by file_id or whatever 
    $path = ... 

    $file = getimagesize($path); 

    $response = new Response(); 
    $response->setContent(file_get_contents($path)); 
    $response->headers->set('Content-Type', $file['mime']); 
    $response->headers->set('Content-Disposition', 'attachment; filename="' . $filename . '"'); 

    return $response; 
} 
+0

在我的项目中,uploadDir从文件要看。为了定义路径,我必须在我的对象内部调用方法getUploadDir()。但该方法受到保护。它一定会公开吗?或者,否则,我该怎么办? –

0
$response=new Response(); 
$response->setContent(file_get_contents($localFilePath)); 

对于强行下载,你可以使用这个:

$file_url = 'http://www.myremoteserver.com/file.exe'; 
header('Content-Type: application/octet-stream'); 
header("Content-Transfer-Encoding: Binary"); 
header("Content-disposition: attachment; filename=\"" . basename($file_url) . "\""); 
readfile($file_url); // do the double-download-dance (dirty but worky) 
+0

在我的项目中,uploadDir依赖于文件。为了定义路径,我必须在我的对象内部调用方法getUploadDir()。但该方法受到保护。它一定会公开吗?或者,否则,我该怎么办? –