title: javascript: downloaded file size different from content length
body: <p>I have a base64 string which I decoded and wishes to allow the user to save this as a file. In particular, when I check the length of decodedContent, it's 11271 bytes.</p>
  
  <pre><code>  var content = messageObj['data'];
  var decodedContent = atob(content);
  console.log(decodedContent.length);
  </code></pre>
  
  <p>Then I used</p>
  
  <pre><code>var blob = new Blob([decodedContent], {type: 'application/octet-stream'});
  window.open((window.URL || window.webkitURL).createObjectURL(blob));
  </code></pre>
  
  <p>To prompt the user to save <code>decodedContent</code>. When I check the file size saved, it says 16892 bytes, which is different from what is stated above. Any idea why?</p>
  
  <p>Content is a base64 encoded tar-ball file sent from the server.</p>
  
  <pre><code>for i ,l in enumerate(to_download):
              if i == 1:
                  break
              last_index = l.rfind('|')
              download_path = l[last_index+1:].strip()
              mda_url = '%s/%s'%(root_url, download_path)
  
              logger.debug('Downloading file %s/%s at %s', i, len(to_download), mda_url)
  
              mda_req = urllib2.Request(mda_url)
              mda_response = urllib2.urlopen(mda_req)
              f = StringIO.StringIO(mda_response.read())
  
              replace_path = mda_url.replace('/', '_')
              ti = TarInfo("%s.txt" %replace_path)
              ti.size = f.len
  
              tar.addfile(ti, f)
  
          tar.close()
          tar_file.close()
  
          with open("/Users/carrier24sg/Desktop/my.tar", 'rb') as f:
              tar_str = f.read()
          logger.info("Completed downloading all the requested files..")
  
  
  
      return tar_str
  </code></pre>
  
  <p><strong>UPDATE</strong>:</p>
  
  <p>Narrowed down to the problem being with either var <code>decodedContent = atob(content)</code>; or <code>var blob = new Blob([decodedContent], {type: 'application/octet-stream'});</code> </p>
  
  <p>Finally I managed to use the @Jeremy Bank's answer <a href="https://stackoverflow.com/questions/16245767/creating-a-blob-from-a-base64-string-in-javascript">here</a>. His first answer solves the issue of content length being different, but when I check the checksum, the content doesn't seem to tally. Only using his second answer's function b64toBlob did I get to resolve this. However, I'm still not sure what is wrong here, so I'm hoping someone can shed some light to this.</p>