【问题标题】:How to Display content of json/csv file on HTML page?如何在 HTML 页面上显示 json/csv 文件的内容?
【发布时间】:2018-12-18 19:08:30
【问题描述】:

使用烧瓶,我在 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 标头进行响应。 developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/…
  • @Jerinaw 这行得通 - 返回 send_file(abs_path, mimetype="application/json")。但是怎么才能让它在html页面中jsonify呢?
  • 顺便说一句,你的 cmets 帮助了我。
  • 酷,给评论投票。如果要格式化 JSON,则必须编写一些将 JSON 转换为 HTML 的 JS。或者如果 JSON 有一些文本格式已经使用 HTML
     标签。
  • 在这篇文章中没有看到upvote flag :(

标签: javascript html flask flask-restful


【解决方案1】:

在提供 json 文件时,您需要使用 text/html 的 Content-Type 标头进行响应。

send_file(abs_path + '/jsonfile.json', mimetype="text/html")

https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-17
    • 1970-01-01
    • 2012-05-28
    • 1970-01-01
    • 2017-03-07
    相关资源
    最近更新 更多