如何在 HTML 页面上显示 json/csv 文件的内容?
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
header 进行响应。
send_file(abs_path + '/jsonfile.json', mimetype="text/html")
https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types
使用 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
header 进行响应。
send_file(abs_path + '/jsonfile.json', mimetype="text/html")
https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types