Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby Net::FTP Progress Bar

Tags:

ruby

ftp

Does anyone know of a way to get a status update from ruby's Net::FTP library while downloading a file? I am trying to implement a web interface that shows a progress bar for percentage remaining when downloading a file from a remote ftp server.

like image 541
smnirven Avatar asked Nov 25 '09 04:11

smnirven


1 Answers

For future reference - I stumbled upon a solution:

filesize = ftp.size(filename)
transferred = 0
p "Beginning download, file size: #{filesize}"
ftp.getbinaryfile(filename, "#{SOURCE_IMPORT_DIRECTORY}/#{filename}", 1024) { |data|
  transferred += data.size
  percent_finished = ((transferred).to_f/filesize.to_f)*100
  p "#{percent_finished.round}% complete"
}
ftp.close
like image 138
smnirven Avatar answered Sep 17 '22 20:09

smnirven