5

I need to save a remote file a cloud storage server,so I must read this file to a file stream,I found this article : Open an IO stream from a local file or url the answer is :

require 'open-uri'
file_contents = open('local-file.txt') { |f| f.read }
web_contents  = open('http://www.stackoverflow.com') {|f| f.read }

But the web_contents is not right.Then I compare this action to a custom local file upload,which format is ASCII-8BIT,the format is not same.so How can I get the correct stream from remote file .

lucianosousa
  • 8,144
  • 4
  • 21
  • 25
HXH
  • 1,643
  • 4
  • 19
  • 31

1 Answers1

5

Seems all right to me:

require 'open-uri'
web_contents  = open('http://www.stackoverflow.com') {|f| f.read }

out_file = File.expand_path("~/Desktop/out.html")

File.open(out_file, "w") do |f|
  f.puts web_contents
end
tompave
  • 11,952
  • 7
  • 37
  • 63