简体   繁体   English

Python Flask - 如何使用 send_from_directory 下载文件并在浏览器中显示文本 output

[英]Python Flask - How to download a file with send_from_directory AND display text output in browser

I'd like to be able to display a message to the user after a download is triggered via send_from_directory.我希望能够在通过 send_from_directory 触发下载后向用户显示一条消息。

@app.route('/downloadFile', methods=['GET'])
def download():

    return send_from_directory(output_directory, "myfile.txt", as_attachment=True)

This will trigger a download after the /downloadFile endpoint is visited, but in addition to the download being triggered, I'd like to have output in the browser that says something like "file download triggered".这将在访问 /downloadFile 端点后触发下载,但除了触发下载外,我还希望浏览器中的 output 显示“文件下载已触发”之类的内容。 Is there a way to achieve this?有没有办法做到这一点? If I wasn't using send_from_directory, I could do something like this to return some JSON return {'Status': 'file download triggered'}如果我没有使用 send_from_directory,我可以做这样的事情来返回一些 JSON return {'Status': 'file download triggered'}

I'm looking for a way to combine the two, that way the user doesn't see a blank screen when they hit this endpoint.我正在寻找一种将两者结合起来的方法,这样用户在点击此端点时就不会看到空白屏幕。

What you could do is, return render_template() at the given endpoint to render an html file which would display something like "your file is now being downloaded" or whatever message you want and in the same html file, use a JS to trigger a 2nd endpoint which would download the file.您可以做的是,在给定端点返回 render_template() 以呈现 html 文件,该文件将显示类似“您的文件正在下载”或您想要的任何消息,并在同一个 html 文件中,使用 JS 触发将下载文件的第二个端点。

@app.route('/downloadPage', methods=['GET'])
def download_page():
    return render_template("download_page.html")

@app.route('/downloadFile')
def download_file():
    return send_from_directory(output_directory, "myfile.txt", as_attachment=True)

In the HTML file, the JS will be something like -在 HTML 文件中,JS 将类似于 -

<SCRIPT>
   window.location.href = "/DownloadFile";
</SCRIPT>

EDIT 1:编辑 1:

@fpolig01 Here is what I did -> I used save_file from flask created a function which returns this @fpolig01 这是我所做的 -> 我使用 flask 中的 save_file 创建了一个 function 并返回了这个

def download_file():
    return send_file('static/downloads/filename.csv',
                     mimetype='text/csv',
                     as_attachment=True)

After this, in your app.route() code, call this function. something like ->在此之后,在您的 app.route() 代码中,将其称为 function。类似于 ->

@app.route('/downloadFile')
def download_file():
    result = download_file()
    return result

In the JS, you will have to redirect to this URI, it will directly start the download/will display a pop up to save the file.在 JS 中,你必须重定向到这个 URI,它会直接开始下载/会显示一个弹出窗口来保存文件。 the client still stays on the last page only.客户仍然只停留在最后一页。 At least this works for me.至少这对我有用。

Or the least you could do is, create an html page with a download your file button, configure the button to go to this download link and then that would work.或者你至少可以做的是,创建一个带有下载文件按钮的 html 页面,将按钮配置为 go 到此下载链接,然后就可以了。

Update me with what you do告诉我你做了什么

For any request, there can only be a single response.对于任何请求,只能有一个响应。 The HTML page response and the file download response must be their own respective responses to two separate requests. HTML 页面响应和文件下载响应必须是它们各自对两个单独请求的响应。

In order to get the effect you want, you would want two endpoints: one endpoint to return an HTML response, and a second endpoint to handle the file download.为了获得您想要的效果,您需要两个端点:一个端点返回 HTML 响应,第二个端点处理文件下载。

Your HTML response from your first route can contain javascript code to initiate the file download from the second endpoint.来自第一个路由的 HTML 响应可以包含javascript 代码以启动从第二个端点下载文件 An implementation of this might look something close to pushpak ruhil's answer .这个的实现可能看起来接近pushpak ruhil 的答案

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

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