Question

I have base64-encoded variable with binary data (image), and I'm trying to save it using TideSDK, but without PHP (just JS). Ti.Filesystem.getFileStream() + .open() + .write() doesn't work in this case, and I didn't find any working example.

Was it helpful?

Solution

this is not possible at all. I review the code and onyl "string" can be saved to a file. (I checked the code for OSX)

You can add this php function to your index.html

<script type="text/php">
// required for the TideSDK file storage
// normal HTML5 browser ignores this part of code because the type is set to PHP.
// TideSDK with PHP binding run this code
// 
function tideSDK_writeBase64AsBinaryData($fileName, $base64){
   $binary=base64_decode($base64);
   $file = fopen($fileName, "w");
   fwrite($file, $binary);
   fclose($file);
}
</script>

this code didn't corrupt your "normal" HTML or JS stuff. Normal browser ignore this.

in my JS code I have such a logic to store binary data

saveFile: function(fileName, content, contentIsBase64,  successCallback, errorCallback) {
    try{

      if(contentIsBase64){
          tideSDK_writeBase64AsBinaryData(fileName, content);
      }
      else{
          //Doesn't have to exist yet.
          var fileHandle = Ti.Filesystem.getFile(fileName);
          var stream =  Ti.Filesystem.getFileStream(fileHandle);
          stream.open(Ti.Filesystem.MODE_WRITE,false);
          stream.write(content);
          stream.close();
      }

      successCallback({title: fileName});
    }
    catch(e){
        errorCallback();
    }
},

I use this in my desktop version of the "Draw2D touch Designer" project and it is working well.

Greetings

Andreas

OTHER TIPS

Andreas, What you say is completely false. There is a way!

I wrote this function that helps you to encode files to base64 and get the string you need: (don't forget the credits ;) )

function encodeFileto64(path){
    var f = path;
    var file = Ti.Filesystem.getFile(f);
    var stream = file.open(Ti.Filesystem.MODE_READ);
    var obj = stream.read();
    var base = Ti.Codec.encodeBase64( obj );
    base = base.replace(/[\n\r]/g, '');
    return base;
};

This does all the trick without going nuts...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top