Saving byteArray pdf file in titanium

I have a SOAP API that returns me a file divided into pieces encoded in several base64 lines

I cannot save it to the file system without breaking it

This is the pastebin of the whole file, encoded as is, when I download and link 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 don’t understand which one is the right way.

Should I convert back to byteArray string myself? What is the correct way to save it?

Do I need to create a buffer from a string or ...?

+6
source share
2 answers

I think base64enc for the file is invalid or incomplete, I tested it with bash and base64 utils. You can follow these steps.

Copy and paste the base64 line into a file called pdf.base64, then run the following 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 look at the generated binary:

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

Try checking to see if you are performing entire fragments correctly, or just call the guy who creates the api soap.

+1
source

Before you start downloading a file, you need to create a file stream in order to write too, to write to the blob in the wrong way:

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

After creating the HTTPClient stream or socket, and when you get some Base64 data from the service, you need to put this decoded data into Titanium.Buffer . This will probably be included in your onload or onstream in 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 data 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!"); } 

In general, errors occur due to the wrong byte order or data type or write in the wrong order. Obviously, it all depends on the server sending the data in the correct order, and it really is!

You can also consider a version of pump that allows you to transfer from an input stream to an output file stream, minimizing your workload.

+1
source

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


All Articles