无法使用python cgi脚本引发“文件下载”对话框

时间:2013-06-21 21:38:29

标签: python cgi download

我想在客户端窗口中弹出一个下载框(对于sqlite3.db文件)。 我写了这段代码:

#!/usr/bin/env python
import os
# HTTP Header
print "Content-Type:application/octet-stream; name=\"sqlite3.db\"\r\n";
print "Content-Disposition: attachment; filename=\"sqlite3.db\"\r\n\n";

# Actual File Content will go hear.
fo = open("sqlite3.db", "rb")

str = fo.read();
print str

# Close opend file
fo.close()

我遇到错误(从错误日志中复制),提到“IOError:[Errno 2]没有这样的文件或目录:'sqlite3.db'”

我还在/ var / www / cgi-bin /中粘贴了python.py和sqlite3.db 我已经为两者分配了0755 chmod。

我已通过打印Hello world脚本进行配置检查。

知道我的错误是什么?我仍然感到困惑,我应该在哪里提供可下载文件的路径?

1 个答案:

答案 0 :(得分:0)

HTTP标头提前终止。 (print本身写新行)

print "Content-Type:application/octet-stream; name=\"sqlite3.db\"\r\n";
print "Content-Disposition: attachment; filename=\"sqlite3.db\"\r\n\n";

Abobe线应该是:

print 'Content-Type:application/octet-stream; name="sqlite3.db'
print 'Content-Disposition: attachment; filename="sqlite3.db'
print