簡體   English   中英

flask下載文件的html鏈接

[英]flask html link for downloading file

我正在建立一個網站,允許用戶從我的服務器下載文件。 我寫了一條送達文件的路線:

@api.route('/download_results/<path:filename>')
def download_results(filename):
    return send_from_directory(app.config['UPLOAD_FOLDER'], filename, as_attachment=True)

如果我嘗試使用這樣的html標簽:

<a class="btn btn-primary" herf="http://localhost/api/download_results/1234567890/results.zip" download="results.zip">Download results</a>

它不起作用。 但是如果我在jQuery中添加事件監聽器:

$('a').on('click', function(event) {
     event.preventDefault();
     window.location.href = location.origin + '/api/download_results/' + data.parsedData[0];
 });

它可以工作,我可以下載,但是我得到警告:

資源被解釋為文檔,但以MIME類型application / x-zip-compressed傳輸:“ http://localhost/api/download_results/1234567890/results.zip ”。

誰知道我在做什么錯,我該如何解決? 謝謝!

您在錨標記的href屬性中有錯字。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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