问题
I have a small python cgi script that accepts an image upload from the user, converts in into a different format, and saves the new file in a temp location. I would like it to then automatically prompt the user to download the converted file. I have tried:
# image conversion stuff....
print "Content-Type: image/eps\n" #have also tried application/postscript, and application/eps
print "Content-Disposition: attachment; filename=%s\n" % new_filename #have tried with and without this...
print open(converted_file_fullpath).read()
print
I have also tried:
print "Location: /path/to/tmp/location/%s" % new_filename
print
My browser either downloads script.cgi
or script.cgi.ps
. Any help is appreciated.
回答1:
I'm not sure, but have you tried separating actual data from headers by newline? EDIT: writing print "\n"
outputs two newlines, so I think it should be written like that:
print "Content-Type: image/eps"
print "Content-Disposition: attachment; filename=%s" % new_filename
print
print open(converted_file_fullpath).read()
Assuming that new_filename
has some reasonable value I can't see what is wrong here.
回答2:
Turns out, you can use the Location
header for this, but it only worked for me with an absolute link. So,
print 'Location: http://example.com/path/to/tmp/location/%s' % new_filename
print
I know, the cgi spec says that relative links should work for internal redirects, but this is what worked for me...
来源:https://stackoverflow.com/questions/3215623/cgi-download-image-after-generating