Saving byteArray to pdf file in Titanium
Asked Answered
D

2

6

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 ...?

Dinette answered 20/11, 2013 at 10:58 Comment(2)
In all your examples you reference f, which is what? A file? FileStream?Kemper
I editet, i'm giving it a FileDinette
C
1

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.

Cenogenesis answered 20/11, 2013 at 17:28 Comment(0)
K
1

Before you start downloading your file you need to create the file stream to write too, writing to a blob is not the way to go:

// Step 1
var outFileStream = Ti.Filesystem.getFile('outfile.bin').open(Ti.Filesystem.MODE_WRITE);

After creating your HTTPClient or socket stream and when you receive a chunk of Base64 data from the serve, you need to put that decoded data into a Titanium.Buffer. This would probably go into your onload or onstream in an HTPPClient, :

// Step 2
var rawDecodedFileChunk = Ti.Utils.base64decode(fileString);
var outBuffer = Ti.createBuffer({
    byteOrder : // May need to set this
    type :      // May also need to set this to match data
    value: rawDecodedFileChunk
});

Finally you can write the data out to the file stream:

// Step 3
var bytesWritten = outFileStream.write(outBuffer);  // writes entire buffer to stream
Ti.API.info("Bytes written:" + bytesWritten);  // should match data length
if(outBuffer.length !== bytesWritten) {
    Ti.API.error("Not all bytes written!");
}

Generally errors come from having the wrong byte order or type of data, or writing in the wrong order. Obviously, this all depends on the server sending the data in the correct order and it being valid!

You may also want to consider the pump command version of this, which allows you to transfer from input stream to output file stream, minimizing your load.

Kemper answered 20/11, 2013 at 17:26 Comment(1)
Thank you for the complete question, unfortunately the way to write was easy, the error was in the APIDinette
C
1

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.

Cenogenesis answered 20/11, 2013 at 17:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.