base64 decoding in js is not resulting in expected output. Why?

Not applicable

Scenario-

I am getting input as a base64 encoded string of an excel file which later on i am trying to decode it and send it as a multipart request to backend.

Error getting;-

When i am trying to decode the encoded string using common base64 js its getting decoded in some other language.Why is this happening??ideally it should get decoded in ASCII format but its not..

Attaching the encoded string..

Its decoding the encoded string in something below mentioned way

PK!IïáÉü[Content_Types].xml ¢( ÌVÛNã0}Gâ"¿¢Æ-¬V5åañ€X$àL<M¬&¶噖öïwbHAz¡AۗÜì9çŒ=sœáù¼®’4ÎfböE6wÚØ"W½S‘ )«Uå,db(ÎG‡Ç…L8Úb&J"&%æ%Ô
SçÁòÈ؅Z¿†Bz•OTò¸ßÿ-sg,õ¨Á£áŒմ¢ärΟ_”<+’?/óªL(ï+“+b¡rfõ’žMÚåӚ¡Sô”Æ€ê*õÁ0c¸"N…ü”3@…ۑ¾f•rd†¥ñxĩÁЌ|ÕkÜ_ގ`4$w*Эª9w9¯ä³“'ç&éjm—&.QZ+c[Ý+øãd”ñ6èXH“_ÞRÇñžè8Ù¿þ“âÞ¯»—F„YSH‹
°ëvˆ ë˜K@ß»Jѹ€÷Økt

Any help on this? why is this happening?

0 17 3,595
17 REPLIES 17

Is it possible the file has been compressed? ie as zip

@dane knezic no..its just the encoded value of excel file

It's definitely a zip file. It looks like this as it starts with PK and you can see compressed file names.. If you take the decoded data, save it to a file with a zip extension, you can extract the file or open it with an archiver of your choice.

Yes,seems like that only.i tried the way you told but while unzipping the file it says the archive is not readable...then what will be the solution for this?

Try a different archiving tool. It seems to be compatible with 7zip.

@dane knezic what is the solution for decoding of the encoded string excel file?

You don't appear to have an encoded excel file but an encoded archive of files.

Looks like you might be dealing with a Unicode encoding problem. Can you try encoding or decoding it as Unicode before dealing with the base64 decode?

Nope - the output of the decode operation is a ZIP file. Not a unicode issue.

This is not phrased as an answer, but is actually the answer. The decoded data is not a string. The decoded data is apparently a ZIP file. And be aware that the .xlsx file format is actually a ZIP file.

So it seems that the base64-decoding is succeeding, and the result of the decoding operation is a byte stream which is a ZIP file. You cannot coerce or cast that byte stream to a string. It does not work that way.

So I think the original asker needs to adjust his or her expectations about how things ought to work.

@Dino Thanks for the input!!

Got your point...

what will be the best way to do for this scenario if we are getting a base 64 encoded string of an excel file and from apigee side we need to decode it and send as a multipart request to backend?

Any alternate solution for this scenario?

Not applicable

Hi Shubam

Can you please let me know how are you generating the base64 encoding of the xlsx file? Are you aware of this. I would recommend to use a common Python( Zlib) or some JS libraries to achieve the same and then do the base64 decoding. we had a similar requirement , actuallu compressing a JWT in our case, as the permissions stored in the JWT are too huge and by default JWT has base64 encoded data. So we decode then we needed to decompress the file.

Lets say you have variable x which has the encoded data , compressed using zlib in python , you can use something like this to decode it

from json import load as load_json

from zlib import decompress

from base64 import b64decode

load_json(decompress(b64decode(permissions)))

This is one of those ways , but you can achieve this using any standard libraries

@Raghavendra Chamarthy i am getting it as part of service input.Front end is using a java Base64 api to convert that excel file into an encoded base64 string and then send it to us

Not applicable

ok..can you share any details about the Base64 API being used to encode this? in the mean while I will try to replicate this scenario

@Raghavendra Chamarthy i have already attached the encoded string above which front end is sending to us..Their java code is something like below

fis = new FileInputStream(file); 
byte byteArray[] = new byte[(int)file.length()]; fis.read(byteArray);
encodedString = Base64.encodeBase64String(byteArray);

yes - the garbled string you posted in your original question is ... equivalent to the byteArray variable in the above snippet of Java code. This is the point - that byteArray is not a string. It's just a stream or array of bytes. Displaying it will show you rubbish. It actually IS the correct data.

The next question is, what do you want to DO with that data ?

And keep in mind that it is not always necessary that the API proxy decode or modify the payload. If your backend expects the file to be a base64-encoded blob, then.... the API proxy can just pass it through. There's no modification at all.

But it's possible you DO need the API proxy to decode the file.

But please explain what you really want to do. "BAse64-decode a file" ok. But WHY? What do you want to do with the decoded data? Explain in some detail please.

@DinoChiesa-at-Google we are getting a base 64 encoded string and the expectation is we decode the string which is a byte array and pass it to our back end system as a multi part request .

The backend saves it as an attachment