Javascript: file upload size different from content length

I have a base64 string that I decoded and want to allow the user to save this as a file. In particular, when I check the length of the decodedContent, it is 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 the decodedContent . When I check the saved file size, it says 16892 bytes, which is different from what is indicated 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

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

Finally, I managed to use @Jeremy Bank's answer here . His first answer solves the problem when the length of the content is different, but when I check the checksum, the content is not like counting. Only using its second response function b64toBlob, I was able to solve this problem. However, I'm still not sure what is wrong here, so I hope someone can shed some light on this.

+6
source share
1 answer

I think the problem is that atomb () returns the base version of the base64 file. when you request its size, it will return the bytes that it contains.

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

The two things are different because the storage size and font size are not the same thing. And they vary on different platforms.

0
source

Source: https://habr.com/ru/post/979899/


All Articles