Created
October 3, 2011 15:37
-
-
Save filippo/1259388 to your computer and use it in GitHub Desktop.
How to return a file in HTTP with a given name
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'); | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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