There is a Ruby string with the contents of a docx file (MS Office Word document).
You need to save it to a file. Under Windows.
The easiest way does not work:
File.write(filename, contents) The resulting file will not open. Why? How to solve it?
There is a Ruby string with the contents of a docx file (MS Office Word document).
You need to save it to a file. Under Windows.
The easiest way does not work:
File.write(filename, contents) The resulting file will not open. Why? How to solve it?
There is a feature in Windows: the default files open in text mode, converting between \r\n (in the file) and \n (in Ruby) (in the output, as here; when you enter, there are still effects around Ctrl + Z / EOF, see option t ).
Therefore, when writing binary data to a file, it is necessary to explicitly specify b in the file open mode, for example ( c IO.write ; NB: File inherits IO ):
File.write(filename, contents, mode: "wb") ... otherwise, the bare byte 0A will be displayed as two bytes: 0D 0A , and the file will not exactly what was in the original line and it probably will not be read by the required software.
Written based on a comment by the author of the question .
Source: https://ru.stackoverflow.com/questions/848053/
All Articles