title: Nodejs, store bin files as BYTEA into pgsql (corrupted files)
body: <p>For some reason I need to store some files (mostly images or pdfs) into my database (PG 9.2.20). 
  Those files are uploaded by users and when I download them back, they are corrupted.</p>
  
  <ul>
  <li>I'm working with nodejs.</li>
  <li>The column type I store the file is BYTEA.</li>
  </ul>
  
  <p>This is how I store them :</p>
  
  <pre><code>const { files, fields } = await asyncBusboy(ctx.req);
  const fileName = files[0].filename;
  const mimeType = files[0].mimeType;
  const bufferedFile = fs.readFileSync(files[0].path, { encoding: 'hex' });
  const fileData = `\\x${bufferedFile}`;
  
  //Just a basic insert into with knex.raw
  const fileId = await storageModel.create(fields.name, fields.description, fileName, mimeType, fileData, ctx.user);
  </code></pre>
  
  <p>And this is how I retrieve my file :</p>
  
  <pre><code>const file = await storageModel.find(ctx.params.fileId, ctx.user);
  ctx.body = Buffer.from(file.file_bin, 'hex');
  ctx.set('Content-disposition', `attachment; filename=${file.file_name}`);
  </code></pre>
  
  <p>The file is corrupted, and of course, if I look closely, the uploaded file and the one I downloaded are different.
  See hex screenshot, there is some additional data at the start of the downloaded one : <a href="https://imgur.com/a/kTRAB" rel="nofollow noreferrer">http://imgur.com/a/kTRAB</a>   </p>
  
  <p>After some more testing I can tell the problem lies into the koa part, when I put the buffer into the ctx.body. It got corrupted (???)</p>
  
  <p>EDIT : I was working with Swagger UI : <a href="https://github.com/swagger-api/swagger-ui/issues/1605" rel="nofollow noreferrer">https://github.com/swagger-api/swagger-ui/issues/1605</a></p>