I was facing issues in making a Python CGI script which downloads a file. When I run my script, the file does get downloaded, but when I open the downloaded file, it says it has zero bytes in it.
This is the HTML snippet that calls the download file in my main Python CGI File
html += """
<form enctype = "multipart/form-data" action = "download_file.py" method = "get" class='file'>
<h3>%s</h3>
<h4>File name: %s</h4>
<input type='hidden' value=%s name='fname'>
<h3 class='btn btn-info'><a href="%s" download>Download File</a></h3>
</form>
""" % (str(i+1), str(files[i]['name']), str(files[i]['name']), str(os.path.join('tmp', files[i]['name'])))
And this is the code inside download_file.py
import cgi, os
form = cgi.FieldStorage()
filename = form['fname'].value
fullpath = open(os.path.join('cgi-bin', 'tmp', str(filename)), "rb")
print('Content-Type:application/octet-stream; name="%s"' % filename)
print('Content-Disposition: attachment; filename="%s"' % filename)
print("")
with open(filename, 'rb') as fo:
print(open(filename).read())
fo.close()
I hope you could help me in resolving this issue!