6

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.

  var content = messageObj['data'];
var decodedContent = atob(content);
console.log(decodedContent.length);

Then I used

var blob = new Blob([decodedContent], {type: 'application/octet-stream'});
window.open((window.URL || window.webkitURL).createObjectURL(blob));

To prompt the user to save decodedContent. When I check the file size saved, it says 16892 bytes, which is different from what is stated above. Any idea why?

Content is a base64 encoded tar-ball file sent from the server.

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

UPDATE:

Narrowed down to the problem being with either var decodedContent = atob(content); or var blob = new Blob([decodedContent], {type: 'application/octet-stream'});

Finally I managed to use the @Jeremy Bank's answer here. 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.

Community
  • 1
  • 1
goh
  • 27,631
  • 28
  • 89
  • 151
  • What does `blob.size` show? – raina77ow Dec 19 '14 at 07:57
  • blob.size says 16892 – goh Dec 19 '14 at 08:12
  • Ok, can you give an example of what's in `decodedContent`? One explanation is that `atob` results in string that contains multibyte characters. For example, `var p = atob('ddd'); var b = new Blob([p], {type:'application/octet-stream'}); console.log(b.size); /* 3 */ console.log(p.length); /* 2 */` – raina77ow Dec 19 '14 at 09:25
  • @raina77ow, its a base64 encoded tarball file sent fromt he server side. I posted segment of the python code I used. – goh Dec 19 '14 at 09:37
  • Is base64 encoding done on server- or client-side? It's probably the former, as you won't have to decode otherwise; still would be nice to clarify this. My guess is that some part of the whole mechanism treats the source material as a string while it's actually a sequence of bytes (or vice versa). – raina77ow Dec 19 '14 at 09:41
  • sorry for the late reply. Yes its done on the server side – goh Dec 21 '14 at 14:46

1 Answers1

0

I think the problem is that atomb() gives back a basic base64 version of the file. when you ask for the size of it, it will return the bytes it contains.

When you make a blob from the base64 variable and ask for its size, it will return how much space it will fill up on your computer.

The two things are different because file storing size and coded size are not the same thing. And they differ on different platforms as well.

szinter
  • 283
  • 1
  • 8