I was trying to do file download using CGI, It's works fine excpt the downloaded file has the name of the python script file.
My code :
#Source file name : download.py
print "Content-Type:application/octet-stream; name=\"%s\"\r\n" %fileName;
print "Content-Disposition: attachment; filename=\"%s\"\r\n\n" %fileName;
with open(fullPath,'rb') as fo:
data = fo.read();
except Exception as e:
print '<br>Exception :'
Did you copy this from PHP ? (PHP uses
; but Python doesn't need it)
You have too many
\n. In Python
After first header (first
\n\n (with '\n' added by
#!/usr/bin/env python import os import sys fullpath = 'images/normal.png' filename = 'hello_world.png' print 'Content-Type: application/octet-stream; name="%s"' % filename print 'Content-Disposition: attachment; filename="%s"' % filename print "Content-Length: " + str(os.stat(fullpath).st_size) print # empty line between headers and body #sys.stdout.flush() try: with open(fullpath, 'rb') as fo: print fo.read() except Exception as e: print 'Content-type:text/html' print # empty line between headers and body print 'Exception :', e