[英]How to Display content of json/csv file on HTML page?
使用flask,我在HTML頁面中列出了一些JSON / CSV文件。
現在,當我單擊超鏈接時,文件正在下載。 但是我想要的是文件的內容要顯示在網站本身上(並可能在與文件名相同的uri的另一個頁面中)。
這個問題使我受阻。 有人可以回答這里的問題嗎?
燒瓶代碼的一部分
@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>
“ /”頁面的視圖-
提供json文件時,您需要使用text/html
的Content-Type
標頭進行響應。
send_file(abs_path + '/jsonfile.json', mimetype="text/html")
https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Basics_of_HTTP/MIME_types
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.