Python CGI生成后下载图像

Python CGI生成后下载图像,python,cgi,Python,Cgi,我有一个小的python cgi脚本,它接受用户上传的图像,转换成不同的格式,并将新文件保存在临时位置。我希望它能自动提示用户下载转换后的文件。我试过: # image conversion stuff.... print "Content-Type: image/eps\n" #have also tried application/postscript, and application/eps print "Content-Disposition: attachment; filename=

我有一个小的python cgi脚本,它接受用户上传的图像,转换成不同的格式,并将新文件保存在临时位置。我希望它能自动提示用户下载转换后的文件。我试过:

# 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
我也尝试过:

print "Location: /path/to/tmp/location/%s" % new_filename
print

我的浏览器下载script.cgi或script.cgi.ps。非常感谢您的帮助。

我不确定,但您是否尝试过使用换行符将实际数据与标题分离?编辑:写入打印\n输出两个换行符,因此我认为应该这样写:

print "Content-Type: image/eps"
print "Content-Disposition: attachment; filename=%s" % new_filename
print
print open(converted_file_fullpath).read()

假设新的文件名有一些合理的值,我看不出这里有什么错。

结果是,你可以使用位置头来实现这一点,但它只适用于绝对链接。所以

print 'Location: http://example.com/path/to/tmp/location/%s' % new_filename
print

我知道,cgi规范说相对链接应该适用于内部重定向,但这对我来说是有效的…

当浏览器下载到错误的文件名时,文件中有什么数据?这是您期望的数据吗?是的,当它下载script.cgi时,该文件看起来像postscript文件。当它作为script.cgi.ps下载时,我得到了正确的图像,但下载时使用了错误的文件名让我很烦恼…即使从打印中删除了“\n”?打印内容类型:image/eps之前是否有任何输出打印语句?宾果,没有注意到您从打印中删除了\n…谢谢!