title: Reconstructing and files uploaded in SQL Server with python
body: <p>I am working with a SQL Server database table similar to this</p>
  
  <pre><code>USER_ID varchar(50), FILE_NAME ntext, FILE_CONTENT ntext
  </code></pre>
  
  <p>sample data:</p>
  
  <pre><code>USER_ID:      1
  FILE_NAME:    (AttachedFiles:1)=file1.pdf
  FILE_CONTENT: (AttachedFiles:1)=H4sIAAAAAAAAAOy8VXQcy7Ku….
  </code></pre>
  
  <p>Means regular expressions I have successfully isolated the "content" of the FILE_CONTENT field by removing the "(AttachedFiles:1)=" part resulting with a string similar to this:</p>
  
  <pre><code>content_str = "H4sIAAAAAAAAAOy8VXQcy7Ku22JmZmZmspiZGS2WLGa0xc…"
  </code></pre>
  
  <p>My plan was to reconstruct the file using this string to download it from the database. During my investigation process, I found <a href="https://stackoverflow.com/questions/31428368/decoding-binary-to-pdf">this post</a> and proceeded to replicate the code like this:</p>
  
  <pre><code>content_str = 'H4sIAAAAAAAAAO19B0AUR/v33...'
  with open(os.path.expanduser('test.pdf'), 'wb') as f:
      f.write(base64.decodestring(content_str))
  </code></pre>
  
  <p>...getting a TypeError: expected bytes-like object, not str</p>
  
  <p>Investigating further, I found <a href="https://stackoverflow.com/questions/8908287/why-do-i-need-b-to-encode-a-python-string-with-base64">this other post</a> and proceeded like this:</p>
  
  <pre><code>content_str = 'H4sIAAAAAAAAAO19B0AUR/v33...'
  encoded = content_str.encode('ascii')
  with open(os.path.expanduser('test.pdf'), 'wb') as f:
      f.write(base64.decodestring(encoded))
  </code></pre>
  
  <p>...resulting as a successful creation of a PDF. However, when trying to open it, I get an error saying that the file is corrupt.</p>
  
  <p>I kindly ask you for any suggestions on how to proceed. I am even open to rethink the process I've came up with if necessary. Many thanks in advance!</p>