Search code examples
titaniumtidesdk

Write binary file in TideSDK


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.


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