CGI 生成后下载图片

1 投票
2 回答
1986 浏览
提问于 2025-04-16 01:04

我有一个小的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。如果有人能帮帮我,我会很感激。

2 个回答

0

原来,你可以用 Location 这个头信息来实现这个功能,但我发现它只对绝对链接有效。所以,

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

我知道,cgi的规范说相对链接应该可以用于内部重定向,但对我来说,只有绝对链接有效...

0

我不太确定,不过你有没有试过用换行符把实际数据和标题分开?编辑一下:写 print "\n" 会输出两个换行,所以我觉得应该这样写:

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

假设 new_filename 有一个合理的值,我看不出这里有什么问题。

撰写回答