2010-07-09 68 views
1

我有一個小型的python cgi腳本,它接受來自用戶的圖像上傳,轉換成不同的格式,並將新文件保存在臨時位置。我希望它自動提示用戶下載轉換後的文件。我曾嘗試:生成後的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.cgiscript.cgi.ps。任何幫助表示讚賞。

+0

當你的瀏覽器下載到錯誤的文件名,哪些數據是在文件中?這是你期望的數據嗎? – 2010-07-09 19:04:21

+0

是的,當它下載script.cgi時,文件看起來像一個postscript文件。當它作爲script.cgi.ps下載時,我得到正確的圖像,但下載時使用「錯誤」文件名困擾我... – 2010-07-09 19:07:51

回答

1

我不確定,但你有沒有嘗試用換行符分隔頭部的實際數據?編輯:寫print "\n"輸出兩個新行,所以我認爲應該這樣寫的是:

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

假設new_filename具有一定的合理價值也看不出這裏有什麼問題。

+0

nope,仍然嘗試dl'script.cgi' ...: -/ – 2010-07-09 19:02:28

+0

和'new_filename'是正確的值: -/ – 2010-07-09 19:03:15

+0

即使從打印中刪除'\ n'?在「print」Content-Type:image/eps「'之前是否有任何輸出(打印語句)? – cji 2010-07-09 19:05:19

0

原來,您可以使用Location標題做到這一點,但它只適用於我的絕對鏈接。所以,

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

我知道,CGI規範說相對鏈接應該爲內部重定向工作,但是這是對我工作......