unknown
unknown

Reputation: 321

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

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>

Upvotes: 0

Views: 149

Answers (1)

Jonas
Jonas

Reputation: 888

Since you are using an <a> element already, you should use the href attribute with the corresponding url. That way the browser handles the downloading for you and you don't have to request it via axios manually.

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

Upvotes: 3

Related Questions