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 .
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With