【问题标题】:unable to Raise a "File Download" Dialog Box using python cgi script无法使用 python cgi 脚本引发“文件下载”对话框
【发布时间】:2013-06-21 21:38:29
【问题描述】:

为了在客户端窗口中弹出一个下载框(对于 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] No such file or directory: 'sqlite3.db'”

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

我通过打印 Hello world 脚本来检查配置。

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

【问题讨论】:

  • 我假设您没有一个名为“sqlite3.db”的文件位于与调用脚本的目录相同的目录中...尝试打印os.getcwd()
  • 可能是cgi脚本是在servers目录下执行的。

标签: python cgi download


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-02
    • 1970-01-01
    相关资源
    最近更新 更多