<p>I don't think you're going to be able to do this out-of-the-box, but should be stupidly simple with a quick <a href="http://buildwithcraft.com/docs/plugins/introduction" rel="nofollow">plugin</a> that registers its own Twig extension.  See <a href="https://github.com/low/low_regex" rel="nofollow">here</a> for an example.</p>

<p>That extension would add a Twig function that's basically just a simple wrapper for PHP's <a href="http://php.net/manual/en/function.file-get-contents.php" rel="nofollow">file_get_contents</a>.</p>

<p>Then from your template, you'd do something like:</p>

<pre><code>{# Get the file contents #}
{% set contents = file('/path/to/asset.zip') %}

{# output it #}
{{ contents }}
</code></pre>

CC BY-SA 3.0
2017-08-09T10:32:34.423Z
@MikePierce I tried your plugin with asset from external links and it actually worked. Thanks!
Anya Dit
CC BY-SA 3.0
2017-11-25T11:07:58.960Z
@MikePierce I've been using your little plugin on one of my sites and it's been working great. However I've just switched the site to https:// and now the plugin fails. Any ideas?
Lettie
CC BY-SA 3.0
2017-11-25T22:28:02.800Z
@Lettie I haven't used it in a while, but just looked at the code. Only thing I could guess would be something in your config paths to the asset source. What do they look like on your set-up?
Mike Pierce
CC BY-SA 3.0
2015-01-08T10:24:30.747Z
For anyone looking, I'm using this plugin. I made my own fork so that environment variables are parsed properly. https://github.com/someoneandsons/craft-yii-sendfile
Mike Pierce
CC BY-SA 3.0
2015-01-08T16:45:04.110Z
Ahh yeah... forgot we had a sendFile() method!
Brad Bell
CC BY-SA 3.0
2015-01-08T20:49:58.680Z
One thing the plugin wouldn't handle would be assets from external sources (S3 etc), but I think that's a job for another day. For my use case, this did the job.
Mike Pierce
CC BY-SA 3.0
2015-01-07T18:36:44.830Z
2015-01-07T18:36:44.830Z
Brad Bell
<p>I have a template called <code>download</code> that receives an asset ID, looks it up and should force the browser to download the file. I've gotten this far with the <code>{% headers %}</code> tag, but I can't figure out how to read the raw contents of the file and output it.</p>

<pre><code>{% set asset = craft.assets.id(craft.request.getLastSegment()).first() %}
{% header 'Content-Description: File Transfer' %}
{% header 'Content-Type: '~asset.mimetype %}
{% header 'Content-Disposition: attachment; filename='~asset.filename %}
{% header 'Content-Transfer-Encoding: binary' %}
{% header 'Expires: 0' %}
{% header 'Cache-Control: must-revalidate' %}
{% header 'Pragma: public' %}
{% header 'Content-Length: '~asset.size %}
{# How to print raw contents of asset.url ??? #}
</code></pre>

<p>Can anyone help?</p>

<templating><assets><url><filesystem>
Force download of asset file from template
# Force download of asset file from template

(Asked by: username_0 on 2015-01-07)

I have a template called download that receives an asset ID, looks it up and should force the browser to download the file. I've gotten this far with the {% headers %} tag, but I can't figure out how to read the raw contents of the file and output it.
{% set asset = craft.assets.id(craft.request.getLastSegment()).first() %}
{% header 'Content-Description: File Transfer' %}
{% header 'Content-Type: '~asset.mimetype %}
{% header 'Content-Disposition: attachment; filename='~asset.filename %}
{% header 'Content-Transfer-Encoding: binary' %}
{% header 'Expires: 0' %}
{% header 'Cache-Control: must-revalidate' %}
{% header 'Pragma: public' %}
{% header 'Content-Length: '~asset.size %}
{# How to print raw contents of asset.url ??? #}

Can anyone help?

**Tags:** templating, assets, url, filesystem

---

## 1 answers

### Answer by username_1 on 2015-01-07. Score: 3 | Accepted answer

I don't think you're going to be able to do this out-of-the-box, but should be stupidly simple with a quick plugin that registers its own Twig extension.  See here for an example.
That extension would add a Twig function that's basically just a simple wrapper for PHP's file_get_contents.
Then from your template, you'd do something like:
{# Get the file contents #}
{% set contents = file('/path/to/asset.zip') %}

{# output it #}
{{ contents }}

> **Comments:**
> **username_0**: For anyone looking, I'm using this plugin. I made my own fork so that environment variables are parsed properly. https://github.com/someoneandsons/craft-yii-sendfile  
> **username_1**: Ahh yeah... forgot we had a sendFile() method!  
> **username_0**: One thing the plugin wouldn't handle would be assets from external sources (S3 etc), but I think that's a job for another day. For my use case, this did the job.  
> **username_2**: @MikePierce I tried your plugin with asset from external links and it actually worked. Thanks!  
> **username_3**: @MikePierce I've been using your little plugin on one of my sites and it's been working great. However I've just switched the site to https:// and now the plugin fails. Any ideas?  
> **username_0**: @Lettie I haven't used it in a while, but just looked at the code. Only thing I could guess would be something in your config paths to the asset source. What do they look like on your set-up?  

---

