2

there is a space after the word Part in the file name I want to download. It looks like http.get doesn't pass the url_path correctly because you can download the file from the browser without any troubles.

any suggestion how I can download a file if there is a space in the file name?

require 'net/http'

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

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

Net::HTTP.start(url_base) do |http|
  resp = http.get(url_path)
  open("test.flv", "wb") do |file|
    file.write(resp.body)
  end
end
puts "Done."
Radek
  • 13,813
  • 52
  • 161
  • 255

2 Answers2

2

You need to properly encode your URL. Something like:

require 'uri'
val = URI.escape("my parameter value")
jac
  • 9,666
  • 2
  • 34
  • 63
  • @Beaner: could you add this code to your answer? `require 'uri'; puts URI.escape('url to encode')` – Radek Feb 20 '10 at 03:30
1

You could try replacing the space with a + or a %20.

Asaph
  • 159,146
  • 25
  • 197
  • 199