File Reader return result "null"
duduche1 May 28, 2016 6:32 AMHello,
I have made a program how read a file text on the sdcard root. The problem is the filereader function always return null.
The function I use is :
function fnTestModuleQuiz()
{
$("idBoxTexte").set("value", "1.0.5" + " -- fnListDirectory = " + cordova.file.externalRootDirectory);
window.resolveLocalFileSystemURL(cordova.file.externalRootDirectory,
function(dir){
try{
dir.getFile("user.txt", {create:true, exclusive: false},
function(fileEntry) {
try{
fileEntry.file(
function (file) {
$("idBoxTexte").set("value", $("idBoxTexte").get("value") + "\n" + "Fichier INFO = " + JSON.encode(file));
/*
reader.onloadstart = function (evt)
{
$("idBoxTexte").set("value", $("idBoxTexte").get("value") + "\n" + "Fichier onloadstart = " + JSON.encode(evt) + "\n");
},
reader.onprogress = function (evt)
{
$("idBoxTexte").set("value", $("idBoxTexte").get("value") + "\n" + "Fichier onprogress = " + JSON.encode(evt) + "\n");
},
reader.onabort = function (evt)
{
$("idBoxTexte").set("value", $("idBoxTexte").get("value") + "\n" + "Fichier onabort = " + JSON.encode(evt) + "\n");
},
*/
var reader = new FileReader();
reader.onloadend = function (evt)
{
try{
$("idBoxTexte").set("value", $("idBoxTexte").get("value") + "\n" + "Fichier (onloadend) JSON = " + JSON.encode(evt) + "\n");
if (evt.target.result !== undefined || evt.target.result !== null) {
$("idBoxTexte").set("value", $("idBoxTexte").get("value") + "\n" + "Fichier DATA-0 = " + evt.target.result);
} else if (evt.target.error !== undefined || evt.target.error !== null) {
$("idBoxTexte").set("value", $("idBoxTexte").get("value") + "\n" + "Fichier DATA-1 = " + evt.target.error);
} else {
$("idBoxTexte").set("value", $("idBoxTexte").get("value") + "\n" + "Fichier DATA-2 = " + "READER_ONLOADEND_ERR " + READER_ONLOADEND_ERR);
}
}catch(oErr){
alert("<br/>Erreur reader(0) : " + oErr);
}
};
reader.readAsText(file);
},
function(oErr){
try{
alert("<br/>Erreur file(0) : " + oErr);
}catch(oErr){
alert("<br/>Erreur file(0) : " + oErr);
}
}
);
}catch(oErr){
alert("<br/>Erreur getFile(1) : " + oErr);
}
},
function(oErr){
try{
alert("<br/>Erreur getFile(0) : " + oErr);
}catch(oErr){
alert("<br/>Erreur getFile(0) : " + oErr);
}
}
);
}catch(oErr){
alert("<br/>Erreur resolveLocalFileSystemURL(1) : " + oErr);
}
},
function(err){
try{
alert("<br/>Erreur resolveLocalFileSystemURL(0) : " + oErr);
}catch(oErr){
alert("<br/>Erreur resolveLocalFileSystemURL(0) : " + oErr);
}
}
);
}
I see the size of the file, but the filereader return "null"
BUILD :
- PhoneGap (iOS / Android / Windows)
cli-6.1.0 (4.1.0 / 5.1.1 / 4.3.1) - PLUGIN :
| Plugin | Source | Version | Installed | Latest | Platforms |
| cordova-plugin-camera | npm | * | 2.2.0 | n/a | android,ios,winphone |
| cordova-plugin-contacts | npm | * | 2.1.0 | n/a | android,ios,winphone |
| cordova-plugin-device | npm | * | 1.1.2 | n/a | android,ios,winphone |
| cordova-plugin-device-motion | npm | * | 1.2.1 | n/a | android,ios,winphone |
| cordova-plugin-device-orientation | npm | * | 1.0.3 | n/a | android,ios,winphone |
| cordova-plugin-file | npm | * | n/a | n/a | android,ios,winphone |
| cordova-plugin-file-transfer | npm | * | 1.5.1 | n/a | android,ios,winphone |
| cordova-plugin-geolocation | npm | * | 2.2.0 | n/a | android,ios,winphone |
| cordova-plugin-media | npm | * | 2.3.0 | n/a | android,ios,winphone |
| cordova-plugin-media-capture | npm | * | 1.3.0 | n/a | android,ios,winphone |
| cordova-plugin-network-information | npm | * | 1.2.1 | n/a | android,ios,winphone |
| cordova-plugin-whitelist | npm | * | 1.2.2 | n/a | android,ios,winphone |