O P
O P

Reputation: 2365

Download Link with jQuery

<a href="style.css">Download our CSS</a>

When I click on this in Chrome, it opens up the CSS in the browser. How can I force the file to download onclick?

Upvotes: 1

Views: 107

Answers (1)

Rishabh
Rishabh

Reputation: 2021

Special HTTP response headers are required to make the browser force download a file. So for example if you're using PHP a simple example would be this -

<?php
// We'll be outputting a CSS
header('Content-type: text/css');

// It will be called style.css
header('Content-Disposition: attachment; filename="style.css"');

// The CSS is in style.css
readfile('style.css');
?>

It doesn't matter what backend language (PHP/Ruby/Python/Node.js) you're using, just send those specific HTTP headers in the response.

Upvotes: 5

Related Questions