Read contents of a local file into a variable in Rails

前端 未结 3 2088
闹比i
闹比i 2021-02-03 16:54

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}

only giv

相关标签:
3条回答
  • 2021-02-03 16:58

    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.

    0 讨论(0)
  • 2021-02-03 16:59
    data = File.read("/path/to/file")
    
    0 讨论(0)
  • 2021-02-03 17:10

    I think you should consider using IO.binread("/path/to/file") if you have a recent ruby interpreter (i.e. >= 1.9.2)

    You could find IO class documentation here http://www.ruby-doc.org/core-2.1.2/IO.html

    0 讨论(0)
提交回复
热议问题