Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving byteArray to pdf file in Titanium

I have a SOAP API that is returning me a file divided in chunks encoded in several base64 strings

i'm not being able to save it to the file system without breaking it

This is the pastebin of a whole file encoded, as is, once i download and chain the responses.

What is the way to save it correctly?

i tried in many ways

var f = Ti.FileSystem.getFile(Ti.FileSystem.tempDirectory, 'test.pdf');

...

    var blobStream = Ti.Stream.createStream({ source: fileString, mode: Ti.Stream.MODE_READ });
    var newBuffer = Ti.createBuffer({ length: fileString.length });

    f.write(fileString);

or

    var data = Ti.Utils.base64decode(fileString);

    var blobStream = Ti.Stream.createStream({ source: data, mode: Ti.Stream.MODE_READ });
    var newBuffer = Ti.createBuffer({ length: data.length });
    var bytes = blobStream.read(newBuffer);

    f.write(fileString);

or

    var data = Ti.Utils.base64decode(fileString);

    var blobStream = Ti.Stream.createStream({ source: data, mode: Ti.Stream.MODE_READ });
    var newBuffer = Ti.createBuffer({ length: data.length });
    var bytes = blobStream.read(newBuffer);

    f.write(bytes);

but i'm not understanding which one is the right path

Do I have to convert back to byteArray the string on my own? What is the right way to save it?

Do I have to create a buffer from the string or ...?

like image 809
Zerho Avatar asked Nov 20 '13 10:11

Zerho


1 Answers

I think that the base64enc for the file is not valid or incomplete, I've tested it using bash and base64 utils. You can perform these steps.

Copy and paste the base64 string on a file called pdf.base64 then run this command:

cat pdf.base64 | base64 --decode >> out.pdf

the output file is not a valid pdf.

You can try to encode and decode a valid pdf file to take a look at the generated binary:

cat validfile.pdf | base64 | base64 --decode >> anothervalidfile.pdf

Try to check if you are chainig chunks correctly or simply make a phone call with the guy who build the soap api.

like image 176
Emanuele Rampichini Avatar answered Sep 21 '22 11:09

Emanuele Rampichini