简体   繁体   中英

Rails update Progress Bar from Controller during file upload

I am still rather new to web programming so please bear with me.

A rails 5.0 application currently works just fine and uploads a .csv file containing product inventory. The problem is that if this file is very large it can take more than a minute to upload and the person on the client may think the server crashed or they lost the internet connection.

The current implementation has a view with a form for selecting the file. Once selected, the controller reads in the file. The view is still sitting on the screen but control has shifted to the server at this point.

I would like to have the server send an update to the view every N% of file read in, so the view could display and update a progress bar. I think this is doable through AJAX calls and JSON or Javascript but am not certain about that. Is this a reasonable approach?

From what you described there seems to be 2 ways you can solve this.

1) Use ActionCable and send progress messages out for that specific file. The message can contain a percentage of how much of the file has been processed. You can read on ActionCable and how to get started on that here .

2) You can use ajax calls that happen on an interval. Basically, there would be javascript code that calls a route within your app to check on the progress of the file that you are processing. So in you controller action that processes the file, you will store the progress as you process the file. It can be in Redis , or in memory within the rails app. As long as it is something that is accessible from a different request. It could possibly even be stored in the session even though I haven't tested using it like that before. Your client side javascript would call the route that reports the progress on an interval.

That's my thoughts on it at least. I hope that's helpful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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