Search code examples
pythonhttpcgimime

Wrong filename when pushing a file to a user through HTTP using Python over Apache


I'm adding functionality onto our website so that users can download files stored in a database. The problem is that I cannot properly specify the filename for the user - the user is instead prompted to save the file with the name of the main python script running the website. I am setting the Content-Disposition information but its not working as expected. I've edited the code down to the following which still fails to work:

import sys, os
import mydatabasemodule
PDFReport = [...read file from database ...]
print('Content-Type: application/octet-stream\n')
print('Content-Disposition: attachment; filename=\"mytest.pdf\"\n')
print(report)
sys.stdout.close()

Running this code prompts the user to download the file as mysite.py. The PDF downloads correctly just with the wrong filename.

Can anyone tell what I'm doing wrong here? In the full version of the code, I also set Content-Description and Content-Length but that also fails. The files are small and I am trying to avoid saving them to disk but even when I do so, the same problem happens.

[edit] The webserver is running CentOS 5.5, Python 2.4.3, Apache 2.2.3, and mod_python. I've tested this on an Ubuntu 11.04 client using Google Chrome 17.0.963.46 beta and Firefox 13. If I instead try to show the PDF inline:

print('Content-type: application/pdf\n')
print('Content-Disposition: inline; filename=\"mytest.pdf\"\n')
print("Content-Length: %d" % len(report))

then Chrome shows the PDF (with a plugin) and Firefox asks to save the file, recognizing it as a PDF but still with the wrong filename i.e. the filename is still the script name.

[edit] The solution was given below by Mike. I think the problem was the newline I added in the first line above. Since print adds a newline, this second newline signaled the end of the header so the Content-Disposition line was never read. Thanks to all for the quick help!


Solution

  • In python versions < 3.0 print is not a function and automatically adds a newline char. Try this.

    import sys, os
    import mydatabasemodule
    PDFReport = [...read file from database ...]
    print 'Content-Type: application/octet-stream'
    print 'Content-Disposition: attachment; filename="mytest.pdf"'
    print
    sys.stdout.write(PDFReport)
    sys.stdout.flush()