Using flask, I have listed a few JSON/CSV file in HTML page.
Now when I click on the hyperlink, the file is getting downloaded. But what I want is that the content of the file to be displayed on the site itself (and possibly in a different page with uri same as filename).
I am blocked with this issue. Can anyone please answer what is the issue here?
part of the flask code
@app.route('/<path:req_path>')
def dir_listing(req_path):
abs_path = os.path.join(UPLOAD_FOLDER, req_path)
# Check if path is a file and serve
if os.path.isfile(abs_path):
return send_file(abs_path)
# Show directory contents
files = os.listdir(abs_path)
return render_template('file_list.html', files=files)
file_list.html
<ul>
{% for file in files %}
<li><a href="{{ file }}">{{ file }}</a></li>
{% endfor %}
</ul>
View of the '/' page -
You need to respond with a Content-Type
header of text/html
when serving the json files.
send_file(abs_path + '/jsonfile.json', mimetype="text/html")
https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types