簡體   English   中英

如何在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> 

“ /”頁面的視圖-

html頁面視圖

提供json文件時,您需要使用text/htmlContent-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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM