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 user click the links. (standard click to download feature)
I am new to Symfony2 and am getting around the whole routing/controller concept, but how would one create a link to such files while still adhering to the MVC? Does one need to set up routing with a controller or does twig have features which allow it etc.
PS: I have read questions such as How to create a download link in Symfony2? but i do not get if they did something in the routing or just added links etc.
Thank you,
Sample implementation would be,
Create a route,
And then in your controller,
For generating download link check Generating urls section of the doc.
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.
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:
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: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.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 calledgetDownloadFileName()
which just returns the file name I want to assign to thedownload
attribute.So basically, this is how I implemented it on my Symfony project's twig template
Don't know if this fits you, but keep this another ultra simple alternative in mind:
i.e. in your view:
It opens the file, and the user has the decision if he wants to print it, download it... etc