我有一个Dropbox,它可以执行块式上传,使用红宝石-进度条作为上传进度的指示符。
当文件小于4MB (块上传的默认块大小)时,它工作得很好,但是它在任何方面都有问题:
from /opt/rubies/2.0.0-p451/lib/ruby/gems/2.0.0/gems/ruby-progressbar-1.2.0/lib/ruby-progressbar/components/progressable.rb:45:in `progress='
from /opt/rubies/2.0.0-p451/lib/ruby/gems/2.0.0/gems/ruby-progressbar-1.2.0/lib/ruby-progressbar/base.rb:138:in `with_progressables'
from /opt/rubies/2.0.0-p451/lib/ruby/gems/2.0.0/gems/ruby-progressbar-1.2.0/lib/ruby-progressbar/base.rb:45:in `block in progress='
from /opt/rubies/2.0.0-p451/lib/ruby/gems/2.0.0/gems/ruby-progressbar-1.2.0/lib/ruby-progressbar/base.rb:148:in `with_update'
from /opt/rubies/2.0.0-p451/lib/ruby/gems/2.0.0/gems/ruby-progressbar-1.2.0/lib/ruby-progressbar/base.rb:45:in `progress='
from /Users/peterso/Projects/slipsquare/lib/slipsquare/middleware/chunked_upload.rb:20:in `block in call'
from /opt/rubies/2.0.0-p451/lib/ruby/gems/2.0.0/gems/dropbox-api-petems-1.1.0/lib/dropbox-api/client/files.rb:50:in `chunked_upload'我想我是在做一些愚蠢的计算,我仍然在添加进度栏的总数,即使上传已经达到完整的大小和完成。
但是我已经看了很长一段时间了,我似乎找不到一种方法,从栏中获得当前的进展,然后“如果progressbar.current_size +偏移量>总计,完成进度栏”。
代码如下所示:
file_name = env['chunked_upload_file_name']
contents = File.open(env['chunked_upload_file_name'])
total_size = File.size(env['chunked_upload_file_name'])
say "Total Size: #{total_size} bytes"
upload_progress_bar = ProgressBar.create(:title => "Upload progress",
:format => '%a <%B> %p%% %t',
:starting_at => 0,
:total => total_size)
response = env['dropbox-client'].chunked_upload file_name, contents do |offset, upload|
upload_progress_bar.progress += offset
end发布于 2014-10-31 13:11:34
在每次迭代中将当前的offset添加到progress中。想象一下,您有一个10K文件,并将其上传成10块。在第一个迭代中,我们的offset是0,在下一个1中,在第三个2中,然后是3。由于您总结了offsets,您的progress将显示60%,尽管它只做了40%。
与其将offset添加到progress中,不如将progress设置为当前的offset
upload_progress_bar.progress = offset或者更正确,因为偏移量告诉在当前块上传之前上载了什么。
upload_progress_bar.progress = offset + default_chunk_sizehttps://stackoverflow.com/questions/26673514
复制相似问题