Search code examples
javascriptjqueryajaxsamsung-smart-tv

File API writing file doesn't work Samsung smart tv SDK


I have this javascript code which makes possible writing in a file

{

     var fileSystemObj = new FileSystem();
     var fileObj = fileSystemObj.openCommonFile(curWidget.id + 
                                           ‘/testFile.data’, ‘w’);
     fileObj.writeLine(‘something to write.’);
     fileSystemObj.closeCommonFile(fileObj);

}

but it doesn't work. Doesn't even display any error!


Solution

  • samsung developer forum (you may not see unless you sign in... ) I am quoting it.

        case tvKey.KEY_RED:
      alert('RED BUTTON!');
      alert('CWID: '+curWidget.id);
      try {
        var fileSystemObj = new FileSystem();
        var fileObj = fileSystemObj.openCommonFile(curWidget.id+'/testFile.data','w');
        fileObj.writeLine('something to write.');
        fileSystemObj.closeCommonFile(fileObj);
      } catch (e) {
        alert('Error: file handling: '+e);
      }
      break;
    

    lead to error: alert() : Error: file handling: TypeError: 'null' is not an object (evaluating 'fileObj.writeLine') Reading cause same problem.

    and solution accepted in that link is:

    I suppose that problem is that you have to create common dir (if does not exist ) at first :

    var fileObj = fileSystemObj.openCommonFile(filePath, 'w');
    if(!fileObj){
    var bValid = fileSystemObj.isValidCommonPath(curWidget.id); 
    if (!bValid) { 
            fileSystemObj.createCommonDir(curWidget.id);    
    }
    }
    fileObj = fileSystemObj.openCommonFile(filePath, 'w');
    fileObj.writeLine('something to write.');
    fileSystemObj.closeCommonFile(fileObj);