我需要将远程文件保存为云存储服务器,所以我必须将此文件读取到文件流中,我发现这篇文章:Open an IO stream from a local file or url的答案是:
require 'open-uri'
file_contents = open('local-file.txt') { |f| f.read }
web_contents = open('http://www.stackoverflow.com') {|f| f.read }
但web_contents
不对。然后我将此操作与自定义本地文件上传进行比较,格式为ASCII-8BIT
,格式不相同。所以如何从远程文件中获取正确的流。
好像对我来说:
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