How to create a link to download generated documents in symfony2?

Documents are created by the system and saved in the folder / web download. I created a view to display links that allow the user to upload files if the user clicks the links. (standard download button)

I am new to Symfony2 and went around the whole routing / controller concept, but how do I link to such files while still adhering to MVC? Do I need to configure routing using the controller or have a branch with functions that allow this, etc.

PS: I read questions such as How to create a download link in Symfony2? but I don’t understand that they did something in routing or just added links, etc.

Thanks,

+8
php symfony twig
source share
5 answers

Make a sample.

Say your project lives in / www /, so that / www / web / is the document root of your symfony2 application. Now all you are trying to get is at / www / web / over http: // server / .

/www/web/downloads/file.zip can be reached at http: //server/downloads/file.zip by default.

+6
source share

An example implementation will 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; } 

To create a link to a download link Create URLs in a document.

+34
source share

This is the best solution that I have come up with so far, it allows you to upload files from outside your "/ var / www / web /" folder, which makes the file inaccessible without running this script used to serve the file.

Thus, you can check if the bootloader has permission to download the file that it wants.

In this example, I used "/ var / www / downloads /", where I store all the files that I want to use as downloads:

 /** * 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)

+5
source share

Just adding the file path to the href attribute didn't work for me.

When a button is clicked, it simply displays the file without downloading it.

Which worked for me, but adds the download attribute to my link, which is an HTML5 attribute. Just add the attribute like this:

 <a href="path/to/file" download>Download Link</a> 

When you click on the link, it will simply download the file without code on the server side.

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, and not the one used when it was stored on the server.

I followed a tutorial on the Symfony website regarding file upload processing. It was useful to me when I figured out how to create a link to download a file. I just added a method to the Document object called getDownloadFileName() , which simply returns the name of the file that I want to assign to the download attribute.

So basically this is how I implemented it on my symfony project twig template

 <a href="{{ asset(file.webPath) }}" download="{{ file.downloadFileName }}"> Download Link </a> 
+4
source share

I don't know if this suits you, but keep this other super-simple alternative:

i.e. In your opinion:

 <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> 

He opens the file, and the user has a solution if he wants to print it, download it, etc.

0
source share

All Articles