ArgumentError (string contains null byte) when uploading file in rails

3.7k Views Asked by At

I am trying to use the roo gem to run some operations on an excel File. This works perfectly when I do it manually:

file = File.join(Rails.root, 'october_data.xlsx')
spreadsheet = Roo::Excelx.new(file)

However, this does not work when I upload the file via a form:

file = File.read params["Team Changes"]["document"].path
spreadsheet = Roo::Excelx.new(file)

I get the following error:

 ArgumentError (string contains null byte)

There seems to be a lot written about string contains null byte but nothing I've seen seems relevant to what I'm working on. How do I fix this?

1

There are 1 best solutions below

0
On

This happened to me. Apparently it is an encoding issue. Try specifying encoding to 'ASCII-8BIT' when you read the file.

file = File.read(params["Team Changes"]["document"].path, encoding: 'ASCII-8BIT')
spreadsheet = Roo::Excelx.new(file)