Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby read remote file to stream

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 .

like image 728
HXH Avatar asked Aug 16 '14 18:08

HXH


1 Answers

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
like image 51
tompave Avatar answered Oct 19 '22 23:10

tompave