Write StringIO to Tempfile Write StringIO to Tempfile ruby ruby

Write StringIO to Tempfile


You're super close:

file.binmodefile.write stringIo.read

open(url) is just opening the stream for reading. It doesn't actually read the data until you call .read on it (which you can then pass in to file.write).


You could also create your tempfile with the correct encoding, like so:

file = Tempfile.new(['temp','.jpg'], :encoding => 'ascii-8bit')

This is the same as setting the file to binmode.