rails如何知道send_file何时完成

时间:2023-02-06 00:15:06

As it takes some time to prepare the content of the data to be downloaded, I want to show a message "Preparing file to download" when the user submits the request

由于准备要下载的数据内容需要一些时间,所以我想在用户提交请求时显示一条消息“准备要下载的文件”

Then when the file is ready, I use send_file to send the data

然后当文件准备好时,我使用send_file发送数据。

Once it's done, I need to clear the message

一旦完成,我需要清除信息

Thanks a lot

非常感谢

1 个解决方案

#1


5  

Here is what I would do.

这就是我要做的。

1. Create a prepare_file action

1。创建一个prepare_file行动

First, I would create an action whose job would just be to create the file and rather than rendering HTML, it would render a JSON object with the name of the file that was created.

首先,我将创建一个操作,它的任务只是创建文件,而不是呈现HTML,它将呈现一个JSON对象,并使用创建的文件的名称。

2. Use AJAX to call the prepare_file action

2。使用AJAX调用prepare_file操作

On the client, when the user clicks to download the file, you display the message, "Preparing download..." and just do an AJAX request to that action. The response you'll get back via AJAX is the name of the file created.

在客户端,当用户单击下载该文件时,您将显示该消息,“准备下载……”并对该操作执行AJAX请求。您将通过AJAX返回的响应是创建的文件的名称。

3. Redirect to the file download

3所示。重定向到文件下载。

Finally, you can hide the preparing download message and redirect the browser to the file download via JavaScript with the name of the file that was created. You would use send_file in this action.

最后,您可以隐藏准备下载消息,并通过JavaScript将浏览器重定向到文件下载,并使用创建的文件的名称。您将在此操作中使用send_file。

I know that, in the question, you also wanted to be able to display to the user a message when the file is downloading and another message when it is finished. However, this isn't possible unless you write your own client-side download manager. The browser handles file downloads entirely and the user will see in the browser that the file is downloading and what the progress is. So, I understand where you're coming from, but you shouldn't feel like the user isn't being informed of what's happening.

我知道,在这个问题中,您还希望能够在文件下载时向用户显示一条消息,在文件完成时显示另一条消息。但是,这是不可能的,除非您编写自己的客户端下载管理器。浏览器完全处理文件下载,用户将在浏览器中看到文件正在下载以及进度。所以,我理解您来自哪里,但是您不应该觉得用户没有被告知正在发生的事情。

At least with this solution, you're displaying a message to them when the file is being prepared and then once that message disappears, they'll get the download file dialog from the browser.

至少在这个解决方案中,您在准备文件时向他们显示消息,一旦消息消失,他们将从浏览器获得下载文件对话框。

If you need help with actual code samples of how to do this, let me know.

如果您需要关于如何执行此操作的实际代码示例的帮助,请让我知道。

#1


5  

Here is what I would do.

这就是我要做的。

1. Create a prepare_file action

1。创建一个prepare_file行动

First, I would create an action whose job would just be to create the file and rather than rendering HTML, it would render a JSON object with the name of the file that was created.

首先,我将创建一个操作,它的任务只是创建文件,而不是呈现HTML,它将呈现一个JSON对象,并使用创建的文件的名称。

2. Use AJAX to call the prepare_file action

2。使用AJAX调用prepare_file操作

On the client, when the user clicks to download the file, you display the message, "Preparing download..." and just do an AJAX request to that action. The response you'll get back via AJAX is the name of the file created.

在客户端,当用户单击下载该文件时,您将显示该消息,“准备下载……”并对该操作执行AJAX请求。您将通过AJAX返回的响应是创建的文件的名称。

3. Redirect to the file download

3所示。重定向到文件下载。

Finally, you can hide the preparing download message and redirect the browser to the file download via JavaScript with the name of the file that was created. You would use send_file in this action.

最后,您可以隐藏准备下载消息,并通过JavaScript将浏览器重定向到文件下载,并使用创建的文件的名称。您将在此操作中使用send_file。

I know that, in the question, you also wanted to be able to display to the user a message when the file is downloading and another message when it is finished. However, this isn't possible unless you write your own client-side download manager. The browser handles file downloads entirely and the user will see in the browser that the file is downloading and what the progress is. So, I understand where you're coming from, but you shouldn't feel like the user isn't being informed of what's happening.

我知道,在这个问题中,您还希望能够在文件下载时向用户显示一条消息,在文件完成时显示另一条消息。但是,这是不可能的,除非您编写自己的客户端下载管理器。浏览器完全处理文件下载,用户将在浏览器中看到文件正在下载以及进度。所以,我理解您来自哪里,但是您不应该觉得用户没有被告知正在发生的事情。

At least with this solution, you're displaying a message to them when the file is being prepared and then once that message disappears, they'll get the download file dialog from the browser.

至少在这个解决方案中,您在准备文件时向他们显示消息,一旦消息消失,他们将从浏览器获得下载文件对话框。

If you need help with actual code samples of how to do this, let me know.

如果您需要关于如何执行此操作的实际代码示例的帮助,请让我知道。