[英]Why can not download the file at Flask?
為什么我不能下載文件?
@app.route('/files/<file>/download')
def download_file(file):
return send_file(uploads_dir+'/files/'+file, as_attachment=True, attachment_filename=file)
127.0.0.1 - - [15/Oct/2019 16:16:55] “GET /files/Screenshot_11.png/download HTTP/1.1”200
fleDownload=event=>{
axios.get(document.getElementById('filename').getAttribute('data-url') + '/download')
}
render() {
return (
<>
<a href="#" id="filename" onClick={this.fleDownload} data-url={this.state.version.file}>{this.state.version.file}</a>
由於您已經在使用<a>
元素,因此您應該將href
屬性與相應的 url 一起使用。 這樣瀏覽器會為您處理下載,您不必手動通過 axios 請求它。
<a href={this.state.version.file + '/download'}>{this.state.version.file}</a>
編輯:使用當前方法(從服務器獲取路徑),瀏覽器不知道您想要下載文件。 您可以通過使用此處提到的方法來規避此問題: https://gist.github.com/javilobo8/097c30a233786be52070986d8cdb1743但這與原始答案相同。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.