title: How do I embed an uploaded binary files (ASCII-8BIT) in an XML (UTF-8)?
body: <p>I have a file which is uploaded via a regular <code>form_for</code>, this gives me a <code>ActionDispatch::Http::UploadedFile</code> object in the params hash on which I can call <code>.read</code> to get the content. I now need to embed the file in an XML document. I'm using a regular Ruby string for now to construct the XML. The default encoding for a Rails string is utf-8.</p>
  
  <p>Therefore I get the error <code>Encoding::UndefinedConversionError</code>, <code>"\x89" from ASCII-8BIT to UTF-8</code>.</p>
  
  <p>This happens for the following files:</p>
  
  <pre>
  what-matters-now-1.pdf: application/octet-stream; charset=binary
  example.csv: text/plain; charset=utf-8
  investigations.png: image/png; charset=binary
  </pre>
  
  <p>It does not happen for:</p>
  
  <pre>my_test.txt: text/plain; charset=us-ascii</pre>
  
  <p>I have tried changing the encoding, but I get the same error:</p>
  
  <pre><code>params[:file].read.encode('utf-8')
  </code></pre>