下载之前获取文件大小&计算已经下载了多少(http + ruby​​) [英] Get file size before downloading & counting how much already downloaded (http+ruby)

查看:139
本文介绍了下载之前获取文件大小&计算已经下载了多少(http + ruby​​)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




  • 在之前获取文件大小,我开始下载

  • 显示已经下载了多少




$ b

  require'net / http'
require'uri'

url =http://www.onalllevels .com / 2009-12-02TheYangShow_Squidoo_Part 1.flv

url_base = url.split('/')[2]
url_path ='/'+url.split('/' )[3 ..- 1] .join('/')

Net :: HTTP.start(url_base)do | http |
resp = http.get(URI.escape(url_path))
打开(test.file,wb)do | file |
file.write(resp.body)
end
end
putsDone。


解决方案

所以我甚至使用进度条使它工作。 ...

  require'net / http'
require'uri'
require'progressbar'

url =url with some file


url_base = url.split('/')[2]
url_path ='/'+ url.split('/')[3 ..- 1] .join('/')
@counter = 0

Net :: HTTP.start(url_base)do | http |
response = http.request_head(URI.escape(url_path))
ProgressBar#format_arguments = [:title,:percentage,:bar,:stat_for_file_transfer]
pbar = ProgressBar.new(file名称:,response ['content-length']。to_i)
File.open(test.file,'w'){| f |
http.get(URI.escape(url_path))do | str |
f.write str
@counter + = str.length
pbar.set(@counter)
end
}
end
pbar .finish
放完成。


Can anybody help me to

  • get the file size before I start downloading
  • display how much % was already downloaded

.

require 'net/http'
require 'uri'

url = "http://www.onalllevels.com/2009-12-02TheYangShow_Squidoo_Part 1.flv"

url_base = url.split('/')[2]
url_path = '/'+url.split('/')[3..-1].join('/')

Net::HTTP.start(url_base) do |http|
  resp = http.get(URI.escape(url_path))
  open("test.file", "wb") do |file|
    file.write(resp.body)
  end
end
puts "Done."

解决方案

so I made it work even with the progress bar ....

require 'net/http'
require 'uri'
require 'progressbar'

url = "url with some file"


url_base = url.split('/')[2]
url_path = '/'+url.split('/')[3..-1].join('/')
@counter = 0

Net::HTTP.start(url_base) do |http|
  response = http.request_head(URI.escape(url_path))
  ProgressBar#format_arguments=[:title, :percentage, :bar, :stat_for_file_transfer]
  pbar = ProgressBar.new("file name:", response['content-length'].to_i)
  File.open("test.file", 'w') {|f|
    http.get(URI.escape(url_path)) do |str|
      f.write str
  @counter += str.length 
  pbar.set(@counter)
    end
   }
end
pbar.finish
puts "Done."

这篇关于下载之前获取文件大小&计算已经下载了多少(http + ruby​​)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆