有什么办法可以停止使用HTTP标头在CGI python中下载文件:内容处置?

时间:2018-08-24 10:38:27

标签: python cgi contenttype

我想在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.

0 个答案:

没有答案