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.
source share