Is there any way to stop downloading a file in CGI python using HTTP header: Content-disposition?

136 Views Asked by At

I want to raise a download box in CGI python and at the same time, I want to display an error message on the page if the file is not present on the server.

It's like using two Content-Type on a single page.

# 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()

This code downloads the file named download.txt every time even if the file is not present on the server. (In this case, bitnami.txt is not present on the server.) And the download.txt contains:

"Content-Type:text/html"
<h1>File Not Found</h1>

Using python 3.6 and xampp server.

I just want to clear the following point:

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

There are 0 best solutions below