Ext.device.filesystem.FileEntry.read() - type: "text" returns blank file

115 views Asked by At

I am facing a problem with Ext.device.filesystem.FileEntry.read() method. When I try to read the text/JSON file as text, I get a blank file. Any idea? Looks like readastext method of extjs file reader is buggy.

Following does not work:

   var fileEntry = new Ext.device.filesystem.FileEntry(newFilePath, fileSystem);
    fileEntry.read({
        //type: "binaryString",
        encoding: 'UTF8',
        type: "text",
        //type: "text",
        success: function(fileData) {
            console.log('--- fileData');
            console.log(fileData);
            console.log('//--- fileData');
            self.onUploadJSONSuccess(fileData, fileName, networkId, activityId, section, showWaitScreen, callback);
        },
        failure: function(error) {
            if(showWaitScreen) {
                console.log('Failed to read file: ' + error);
                Ext.Msg.alert('Failed to read file: ' + error);
            }
        }
    });

But if I change the type from "text" to "binaryString", it reads the file but off course mess up the special characters.

   var fileEntry = new Ext.device.filesystem.FileEntry(newFilePath, fileSystem);
    fileEntry.read({
        type: "binaryString",
        success: function(fileData) {
            console.log('--- fileData');
            console.log(fileData);
            console.log('//--- fileData');
            self.onUploadJSONSuccess(fileData, fileName, networkId, activityId, section, showWaitScreen, callback);
        },
        failure: function(error) {
            if(showWaitScreen) {
                console.log('Failed to read file: ' + error);
                Ext.Msg.alert('Failed to read file: ' + error);
            }
        }
    });

Regards, Waheed

1

There are 1 answers

0
Waheed Akhtar On BEST ANSWER

Changing the encoding to 'UTF-8' does the trick. So, working code is below:

    fileEntry.read({
        //type: "binaryString",
        type: "text",
        encoding: "UTF-8",
        success: function(fileData) {
            console.log('--- fileData');
            console.log(fileData);
            console.log('//--- fileData');
            self.onUploadJSONSuccess(fileData, fileName, networkId, activityId, section, showWaitScreen, callback);
        },
        failure: function(error) {
            if(showWaitScreen) {
                console.log('Failed to read file: ' + error);
                Ext.Msg.alert('Failed to read file: ' + error);
            }
        }
    });