Skip to content

Instantly share code, notes, and snippets.

@filippo
Created October 3, 2011 15:37
Show Gist options
  • Save filippo/1259388 to your computer and use it in GitHub Desktop.
Save filippo/1259388 to your computer and use it in GitHub Desktop.
How to return a file in HTTP with a given name
<?php
// We'll be outputting a PDF
header('Content-type: application/pdf');
// and It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');
// The PDF source is in original.pdf
readfile('original.pdf');
?>
def handler(req):
req.content_type = "application/pdf"
req.headers_out.add('Content-Disposition', "attachment; filename=\"downloaded.pdf\"")
data = file('original').read()
req.write(data)
return apache.OK
Suppose you want to return a PDF via http.
The pdf is named original.pdf, but you want to return it as "downloaded.pdf"
We can use the http headers Content-Type and Content-Disposition. See the PHP and mod_python examples below.
This is useful for documents (pdf, word, images, etc...) generated on the fly on the server.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment