tags:

views:

14

answers:

2

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  A: 

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.

cji
nope, still tries to dl `script.cgi`...:-/
W_P
and `new_filename` is the correct value :-/
W_P
Even with '\n' removed from prints? Is there any output (print statements) before `print "Content-Type: image/eps"`?
cji
bingo, didn't notice that you took `\n` out of the prints...thanks!
W_P
A: 

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...

W_P