如何在Rails中保存完整的html页面

时间:2021-06-15 21:22:51

I'm trying to save a webpage from my rails 4 application to disk, using

我正在尝试将一个网页从我的rails 4应用程序保存到磁盘,使用

send_data(render_to_string, :filename => "foo.html").

send_data(render_to_string,:filename =>“foo.html”)。

The file is saved alright, but the css is missing.

文件保存正常,但缺少css。

I tried adding the type attribute, like so:

我尝试添加type属性,如下所示:

send_data(render_to_string, :filename => "foo.html", :type => "text/html")

send_data(render_to_string,:filename =>“foo.html”,:type =>“text / html”)

but it didn't help.

但它没有帮助。

How can I save the file with all the css (and other assets potentially), so that if I click on the saved file I'll see the same thing that I attempted to save?

如何使用所有css(以及其他可能的资产)保存文件,这样如果我点击保存的文件,我会看到我试图保存的相同内容?

2 个解决方案

#1


1  

render_to_string renders only the html part. That returns exact the same string like the browser receives when he loads a html page. Stylesheets and other assets will be loaded in additional requests. Therefore I only see one possible way: render_to_string a html layout with all assets inlined.

render_to_string只呈现html部分。这会返回与浏览器加载html页面时收到的完全相同的字符串。样式表和其他资产将在其他请求中加载。因此,我只看到一种可能的方式:render_to_string一个内联所有资产的html布局。

An other option might be to open the save as dialog with Javascript. But there seems no standardized way to do so.

另一个选项可能是使用Javascript打开另存为对话框。但似乎没有标准化的方法。

#2


0  

You need to use some crawler to get the page with all assets and then gzip and send the result.

您需要使用一些爬虫来获取包含所有资源的页面,然后gzip并发送结果。

#1


1  

render_to_string renders only the html part. That returns exact the same string like the browser receives when he loads a html page. Stylesheets and other assets will be loaded in additional requests. Therefore I only see one possible way: render_to_string a html layout with all assets inlined.

render_to_string只呈现html部分。这会返回与浏览器加载html页面时收到的完全相同的字符串。样式表和其他资产将在其他请求中加载。因此,我只看到一种可能的方式:render_to_string一个内联所有资产的html布局。

An other option might be to open the save as dialog with Javascript. But there seems no standardized way to do so.

另一个选项可能是使用Javascript打开另存为对话框。但似乎没有标准化的方法。

#2


0  

You need to use some crawler to get the page with all assets and then gzip and send the result.

您需要使用一些爬虫来获取包含所有资源的页面,然后gzip并发送结果。