我正在尝试使用 roo gem 在 excel 文件上运行一些操作。当我手动执行时,这非常有效:
file = File.join(Rails.root, 'october_data.xlsx')
spreadsheet = Roo::Excelx.new(file)
但是,当我通过表单上传文件时,这不起作用:
file = File.read params["Team Changes"]["document"].path
spreadsheet = Roo::Excelx.new(file)
我收到以下错误:
ArgumentError (string contains null byte)
关于 string contains null byte
似乎有很多文章,但我所看到的似乎与我正在处理的内容无关。我该如何解决这个问题?
最佳答案
这件事发生在我身上。显然这是一个编码问题。 读取文件时尝试将编码指定为“ASCII-8BIT”。
file = File.read(params["Team Changes"]["document"].path, encoding: 'ASCII-8BIT')
spreadsheet = Roo::Excelx.new(file)
关于ruby - 在 rails 中上传文件时出现 ArgumentError(字符串包含空字节),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45166720/