开发者

rails how to know when send_file done

开发者 https://www.devze.com 2023-01-14 13:29 出处:网络
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 reque开发者_开发知识库st

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 reque开发者_开发知识库st

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

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

Thanks a lot


Here is what I would do.

1. Create a prepare_file action

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.

2. Use AJAX to call the prepare_file action

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.

3. Redirect to the file download

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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消