我想在 CGI python 中提出一个下载框,同时,如果服务器上不存在该文件,我想在页面上显示一条错误消息。
这就像在一个页面上使用两个 Content-Type。
# HTTP Header
print ("Content-Type:application/octet-stream;")
print ("Content-Disposition: attachment; filename = \"download.txt\"\r\n\n")
# Actual File Content will go here.
try:
fo = open("bitnami.txt", "r")
except:
print("Content-Type:text/html\n\r\n")
print("<h1>File Not Found</h1>")
else:
str = fo.read();
print(str)
# Close opened file
fo.close()
此代码每次都会下载名为 download.txt 的文件,即使该文件不存在于服务器上也是如此。(在这种情况下,服务器上不存在 bitnami.txt。)而 download.txt 包含:
"Content-Type:text/html"
<h1>File Not Found</h1>
使用 python 3.6 和 xampp 服务器。
我只想澄清以下几点:
1. Is there any way to use two content headers on a single page.
2. How should I not allow the download of files not present on the server?
3.How to give a link to another page to display the error message in except:
Block.