将 CGI 脚本的输出保存在与脚本名称不同的文件名下
我有一个在服务器上运行的 Python CGI 脚本 script.py
。它生成一个 CSV 文件作为输出。 我希望没有计算机经验、不了解文件扩展名的人能够将文件保存到硬盘上并通过双击来使用它。
问题:如果他们现在在浏览器的保存对话框中单击“确定”,则保存的文件名称为 script.py
,而不是 script.csv
。
如何在 CGI 中设置某种“默认文件名”?也许是一些 HTTP 标头欺骗? 我无权访问服务器配置。
I have a Python CGI script script.py
running on a server. It produces a CSV file as output.
I want computer-inexperienced people that don't know about file extensions to be able to just save the file to their hard drive and use it by double-clicking.
The problem: If they now click "OK" in the save dialog of the browser, the saved file's name is script.py
, instead of script.csv
.
How can I set some sort of "default filename" from within the CGI? Maybe some HTTP header trickery?
I don't have access to the server configuration.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要设置一个
Content-Disposition
标头,其中filename
属性。请参阅之前的问题进行一些讨论。你的看起来像:
除了设置文件名之外,这还将告诉浏览器保存文件,而不是内联打开它。
You need to set a
Content-Disposition
header, with afilename
attribute. See an earlier question for some discussion.Yours would look like:
As well as setting the filename, this will tell the browser to save the file, rather than to open it inline.