2015-09-06 21 views
2

我想写一个比较两个文件内容的方法。我如何加载外部网址/ Internet文件?如何将文件从Internet加载到方法中?

其中一个文件在我的应用程序中,我使用下面的行加载它,这似乎工作。

file1 = File.open('app/assets/files/example.html') 

但第二个文件是在互联网上。下面这行加载文件失败(错误:No such file or directory @ rb_sysopen)。我怎么能加载我的比较方法的Internet文件/页面?

file2 = File.open('http://www.example.com/example.html') 
+1

'需要“开uri''通过。然后'file2 = open(“http:// ....”)'' – mike

回答

3

您可以通过OpenURI轻松实现此目的。

require 'open-uri' 

file2 = open('http://www.example.com/example.html') 

或者,您也可以在URI

file2 = open(URI.parse('http://www.example.com/example.html')) 
相关问题