Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-08-09T10:32:34.423Z
Answers.Text: @MikePierce I tried your plugin with asset from external links and it actually worked. Thanks!
Answers.UserDisplayName: Anya Dit
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-11-25T11:07:58.960Z
Answers.Text: @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?
Answers.UserDisplayName: Lettie
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-11-25T22:28:02.800Z
Answers.Text: @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?
Answers.UserDisplayName: Mike Pierce
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-01-08T10:24:30.747Z
Answers.Text: 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
Answers.UserDisplayName: Mike Pierce
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-01-08T16:45:04.110Z
Answers.Text: Ahh yeah... forgot we had a sendFile() method!
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-01-08T20:49:58.680Z
Answers.Text: 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.
Answers.UserDisplayName: Mike Pierce
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-01-07T18:36:44.830Z
Answers.LastActivityDate: 2015-01-07T18:36:44.830Z
Answers.UserDisplayName: Brad Bell
Body: <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>
  
Tags: <templating><assets><url><filesystem>
Title: Force download of asset file from template
ThreadText: # 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](http://buildwithcraft.com/docs/plugins/introduction) that registers its own Twig extension. See [here](https://github.com/low/low_regex) for an example.
  
  That extension would add a Twig function that's basically just a simple wrapper for PHP's [file\_get\_contents](http://php.net/manual/en/function.file-get-contents.php).
  
  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**: @username_0 I tried your plugin with asset from external links and it actually worked. Thanks!  
  > **username_3**: @username_0 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**: @username_3 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?  
  
  ---
  
  