Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save nepsilon/920106e8f8a5f51d2a65f1d2dcdd389f to your computer and use it in GitHub Desktop.
Save nepsilon/920106e8f8a5f51d2a65f1d2dcdd389f to your computer and use it in GitHub Desktop.
How to force a file download in the browser? — First published in fullweb.io issue #93

How to force a file download in the browser?

The use case is simple, you have /reports/593874951.pdf on your web server and want to let your user download it — and if possible with a meaningful name.

In the past, you may tried using the Content-Disposition HTTP header to achieve this, but today, with Safari getting the support for the download attribute it’s going to simplify a lot of things.

Using the download attribute is simple as pie:

<a href="/reports/593874951.pdf" download="report.pdf">
   Download report
</a>

The browser will then prevent the file to be opened in the browser and force its download, with the name specified in download’s value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment