Documents are created by the system and saved to the folder /web/downloads. I have created a view to display links which will allow a user to download the files, should the
Sample implementation would be,
Create a route,
download_route:
pattern: /download/{filename}
defaults: { _controller: YourBundle:Controller:download }
And then in your controller,
public function downloadAction($filename)
{
$request = $this->get('request');
$path = $this->get('kernel')->getRootDir(). "/../web/downloads/";
$content = file_get_contents($path.$filename);
$response = new Response();
//set headers
$response->headers->set('Content-Type', 'mime/type');
$response->headers->set('Content-Disposition', 'attachment;filename="'.$filename);
$response->setContent($content);
return $response;
}
For generating download link check Generating urls section of the doc.
Don't know if this fits you, but keep this another ultra simple alternative in mind:
i.e. in your view:
<a class='north' href="{{ asset('bundles/TP/Resume.pdf') }}" target="_blank" title="Download.pdf"><img src="{{ asset('bundles/TP/images/icn-save.jpg') }}" alt="Download the pdf version" /></a>
It opens the file, and the user has the decision if he wants to print it, download it... etc
This is the best solution i came up with so far, it allows you to serve downloads from outside of your "/var/www/web/" folder, which makes the file not accessible without running this script used to serve the file.
This way you can check if the downloader has permission to download the file he wants.
In this example i used "/var/www/downloads/" where i store all files i want to serve as a download:
/**
* http://api.symfony.com/2.2/Symfony/Component/HttpFoundation/BinaryFileResponse.html
*/
use Symfony\Component\HttpFoundation\BinaryFileResponse;
class OfflineToolController extends Controller
{
/**
* @return BinaryFileResponse
*/
public function downloadAction()
{
$path = $this->get('kernel')->getRootDir(). "/../downloads/";
$file = $path.'my_file.zip'; // Path to the file on the server
$response = new BinaryFileResponse($file);
// Give the file a name:
$response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT,'my_file_name.zip');
return $response;
}
}
source: docs: http://symfony.com/doc/current/components/http_foundation/introduction.html
(should work on versions above 2.2)
Just adding the path of the file to the href
attribute didn't work for me.
When clicked, it just displays the file without really downloading it.
What worked for me though is adding a download
attribute to my link which is an HTML5 attribute. Just add the attribute like so:
<a href="path/to/file" download>Download Link</a>
Upon clicking the link, it will just download the file without any server side code.
You can also assign a value to the download
attribute.
<a href="path/to/file" download="filename.txt">Download Link</a>
The value of the download
attribute will be used as the file name of the downloaded file instead of the one used while it was stored on the server.
I followed the tutorial in the Symfony website regarding file upload handling. I found it helpful when I was figuring out how to make a download link for the file. I just added a method to the Document
entity called getDownloadFileName()
which just returns the file name I want to assign to the download
attribute.
So basically, this is how I implemented it on my Symfony project's twig template
<a href="{{ asset(file.webPath) }}" download="{{ file.downloadFileName }}">
Download Link
</a>
Let's make a sample.
Say your project lives in /www/, so /www/web/ is the document root of your symfony2 application. Now everything you try to access that is in /www/web/ over http://server/ will show up.
/www/web/downloads/file.zip would be reachable at http://server/downloads/file.zip by default.