简体   繁体   English

为什么不能下载Flask的文件?

[英]Why can not download the file at Flask?

Why can not I download the file?为什么我不能下载文件?

@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 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>

Since you are using an <a> element already, you should use the href attribute with the corresponding url.由于您已经在使用<a>元素,因此您应该将href属性与相应的 url 一起使用。 That way the browser handles the downloading for you and you don't have to request it via axios manually.这样浏览器会为您处理下载,您不必手动通过 axios 请求它。

<a href={this.state.version.file + '/download'}>{this.state.version.file}</a>

Edit: With the current approach (fetching the path from the server) the browser doesn't know that you want the file to be downloaded.编辑:使用当前方法(从服务器获取路径),浏览器不知道您想要下载文件。 You could circumvent this by using the approach mentioned here: https://gist.github.com/javilobo8/097c30a233786be52070986d8cdb1743 but that is just a hack and ultimately does the same as my original answer does.您可以通过使用此处提到的方法来规避此问题: https://gist.github.com/javilobo8/097c30a233786be52070986d8cdb1743但这与原始答案相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM