THIS IS AN EXPERIMENTAL FEATURE

Which means that it doesn’t yet work on all systems. We‘re still working on full compatibility. It’s thus not advised to use this unless you’ve verified it to work fully on all the systems that is a part of your environment. Consider this an extended preview.

Upload Progress abstracts the progress of an upload. It’s used by the multipart progress IO that keeps track of the upload progress and creating the application depends on. It contians methods to update the progress during an upload and read the statistics such as received_bytes, total_bytes, completed_percent, bitrate, and remaining_seconds

You can get the current Progress object by calling upload_progress instance method in your controller or view.

Methods
Constants
MIN_SAMPLE_TIME = 0.150
  Number of seconds between bitrate samples. Updates that occur more frequently than MIN_SAMPLE_TIME will not be queued until this time passes. This behavior gives a good balance of accuracy and load for both fast and slow transfers.
MIN_STALL_TIME = 10.0
  Number of seconds between updates before giving up to try and calculate bitrate anymore
MAX_SAMPLES = 20
  Number of samples used to calculate bitrate
Attributes
[R] last_update_time The last time the upload history was updated
[RW] message A message you can set from your controller or view to be rendered in the upload_status_text helper method. If you set a messagein a controller then call session.update to make that message available to your upload_status action.
[R] received_bytes Number bytes received from the multipart post
[R] total_bytes Total number of bytes expected from the mutlipart post
Public Class methods
new(total)

Create a new Progress object passing the expected number of bytes to receive

     # File vendor/rails/actionpack/lib/action_controller/upload_progress.rb, line 319
319:       def initialize(total)
320:         @total_bytes = total
321:         reset!
322:       end
Public Instance methods
bitrate()

Calculates the bitrate in bytes/second. If the transfer is stalled or just started, the bitrate will be 0

     # File vendor/rails/actionpack/lib/action_controller/upload_progress.rb, line 383
383:       def bitrate
384:         history_bytes, history_time = @history.transpose.map { |vals| vals.inject { |sum, v| sum + v } } 
385:         history_bytes / history_time rescue 0
386:       end
completed_percent()

Completed percent in integer form from 0..100

     # File vendor/rails/actionpack/lib/action_controller/upload_progress.rb, line 337
337:       def completed_percent
338:         (@received_bytes * 100 / @total_bytes).to_i rescue 0
339:       end
elapsed_seconds()

Number of seconds elapsed since the start of the upload

     # File vendor/rails/actionpack/lib/action_controller/upload_progress.rb, line 389
389:       def elapsed_seconds
390:         @last_update_time
391:       end
finished?()

Returns true if there are bytes pending otherwise returns false

     # File vendor/rails/actionpack/lib/action_controller/upload_progress.rb, line 400
400:       def finished?
401:         remaining_bytes <= 0
402:       end
remaining_bytes()

Number of bytes left for this upload

     # File vendor/rails/actionpack/lib/action_controller/upload_progress.rb, line 332
332:       def remaining_bytes
333:         @total_bytes - @received_bytes
334:       end
remaining_seconds()

Calculate the seconds remaining based on the current bitrate. Returns O seconds if stalled or if no bytes have been received

     # File vendor/rails/actionpack/lib/action_controller/upload_progress.rb, line 395
395:       def remaining_seconds
396:         remaining_bytes / bitrate rescue 0
397:       end
reset!()

Resets the received_bytes, last_update_time, message and bitrate, but but maintains the total expected bytes

     # File vendor/rails/actionpack/lib/action_controller/upload_progress.rb, line 326
326:       def reset!
327:         @received_bytes, @last_update_time, @stalled, @message = 0, 0, false, ''
328:         reset_history
329:       end
stalled?()

Returns true if there has been a delay in receiving bytes. The delay is set by the constant MIN_STALL_TIME

     # File vendor/rails/actionpack/lib/action_controller/upload_progress.rb, line 411
411:       def stalled?
412:         @stalled
413:       end
started?()

Returns true if some bytes have been received

     # File vendor/rails/actionpack/lib/action_controller/upload_progress.rb, line 405
405:       def started?
406:         @received_bytes > 0
407:       end