将本地文件的内容读入 Rails 中的变量 [英] Read contents of a local file into a variable in Rails
问题描述
我想要做的就是从本地文件中获取所有内容并将其存储在一个变量中.怎么样?
All I want to do is get all the content from a local file and store it in a variable. How?
File.read(@icon.full_filename).each {|l|r += l}
只给了我一部分.在 PHP 中,我只使用了 file_get_contents
.
only gives me a part of it. In PHP, I just used file_get_contents
.
推荐答案
在这里回答我自己的问题...结果证明这是在读取需要额外标志的二进制文件(在我的情况下为 JPEG)时发生的 Windows 唯一怪癖在 open 或 File.open 函数调用中.我将其修改为 open("/path/to/file", 'rb') {|io|a = a + io.read}
一切都很好.
Answering my own question here... turns out it's a Windows only quirk that happens when reading binary files (in my case a JPEG) that requires an additional flag in the open or File.open function call. I revised it to open("/path/to/file", 'rb') {|io| a = a + io.read}
and all was fine.
这篇关于将本地文件的内容读入 Rails 中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!