How to create a link to download generated documen

2019-01-23 07:05发布

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,

标签: php symfony twig
5条回答
何必那么认真
2楼-- · 2019-01-23 07:40

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.

查看更多
男人必须洒脱
3楼-- · 2019-01-23 07:54

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.

查看更多
贪生不怕死
4楼-- · 2019-01-23 07:55

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)

查看更多
淡お忘
5楼-- · 2019-01-23 07:56

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>
查看更多
霸刀☆藐视天下
6楼-- · 2019-01-23 07:56

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

查看更多
登录 后发表回答