Ich verwende eine Emulation von einem Android Nexus 9 zum Testen.Cordova fileReader funktioniert für Text, gibt aber null für JPS
Ich betreibe diesen Code für Text, und es funktioniert:
var filePath = cordova.file.dataDirectory + "files/newFile.txt";
window.resolveLocalFileSystemURL(filePath, gotFileEntry, fail);
function gotFileEntry(fileEntry) {
console.log("gotFileEntry: "+fileEntry.name); //"newFile.txt"
console.log("fileEntry fullpath: "+fileEntry.fullPath); //"/newFile.txt" - cause for concern?
fileEntry.file(gotFile, fail);
}
function gotFile(file){
console.log("Got the File");
console.log("Type: " + file.type); //text/plain
console.log("Path: " + file.fullPath); //undefined
//path wasn't defined, manually set it
file.fullPath = fullPath = cordova.file.dataDirectory + 'files/' + file.name;
console.log("Path: " + file.fullPath); //"file:///data/data/com.ionicframework.myproject123456/files/files/newFile.txt"
console.log("Bytes: " + file.size); //~14 bytes
var reader = new FileReader();
reader.onloadend = function(evt) {
console.log('Reader status "onloadend": '+reader.readyState); //2 (a.k.a loaded)
console.log('Result: '+evt.target.result); //"data:text/plain;base64,c26tZSBmaWx1IGrhdGE=" (success)
console.log('Result: '+reader.result); //"data:text/plain;base64,c26tZSBmaWx1IGrhdGE=" (success)
};
reader.onerror = function(e) {
console.log('Error.code: '+reader.error.code) //unused
console.log('Error.message: '+reader.error.message) //unused
}
console.log("Reader status before: "+reader.readyState); //0
reader.readAsDataURL(file);
console.log("Reader status after: "+reader.readyState); //1
}
So schalte ich es das Bild, um zu versuchen und zu verwenden. Denselben Code „NEWFILE.TXT“ für „mypicture.jpg“ vermittlungs
var filePath = cordova.file.dataDirectory + "files/mypicture.jpg";
window.resolveLocalFileSystemURL(filePath, gotFileEntry, fail);
function gotFileEntry(fileEntry) {
console.log("gotFileEntry: "+fileEntry.name); //"mypicture.jpg"
console.log("fileEntry fullpath: "+fileEntry.fullPath); //"/mypicture.jpg" - cause for concern?
fileEntry.file(gotFile, fail);
}
function gotFile(file){
console.log("Got the File");
console.log("Type: " + file.type); //image/jpeg
console.log("Path: " + file.fullPath); //undefined
//path wasn't defined, manually set it
file.fullPath = fullPath = cordova.file.dataDirectory + 'files/' + file.name;
console.log("Path: " + file.fullPath); //"file:///data/data/com.ionicframework.myproject123456/files/files/mypicture.jpg"
console.log("Bytes: " + file.size); //~3 mega bytes
var reader = new FileReader();
reader.onloadend = function(evt) {
console.log('Reader status "onloadend": '+reader.readyState); //2 (a.k.a loaded)
console.log('Result: '+evt.target.result); //"null" (FAILURE)
console.log('Result: '+reader.result); //"null" (FAILURE)
};
reader.onerror = function(e) {
console.log('Error.code: '+reader.error.code) //1 (NOT_FOUND_ERR?)
console.log('Error.message: '+reader.error.message) //undefined
}
console.log("Reader status before: "+reader.readyState); //0
reader.readAsDataURL(file);
console.log("Reader status after: "+reader.readyState); //1
}
Mit „adb shell“ Ich bin in der Lage, das Verzeichnis zu zeigen, wo sich die Dateien befinden. Beide Dateien sind da. Die Berechtigungen und der Besitzer beider Dateien sind identisch.
Mein Endziel ist es, eine Datei als base64-Datenstrom einzulesen und sie als Anhang einer E-Mail hinzuzufügen, indem ich das katzer emailComposer-Plugin verwende. Ich hoffe, dass ich diese Methode auch bei einer iOS-Bereitstellung verwenden kann.
Wie lese ich diese Bilddatei falsch - warum ist reader.readyState null?
Vielen Dank im Voraus.
3mb ist viel zu groß für ein DataURL, funktioniert ein kleineres Bild? Ich würde auch 'console.log (e)' in der Error-Handler, um die Details zu erhalten. – dandavis
@dandavis Der Plan war, diese Methode zu verwenden, um bis zu 25 MB Zip-Dateien zu senden - ich muss in die maximale Größe von DataURLs schauen. Ich hatte nicht berücksichtigt, dass es in einem Datenstrom eine maximale Länge geben könnte. Ich bekomme immer "e" undefiniert, also habe ich den error.code verwendet und online nachgeschlagen. Ich bin neu in dieser Sprache. –
Ich dachte nicht, dass die Größe ein Problem basierend auf diesen Artikeln wäre: [link] (http://stackoverflow.com/questions/20706418/are-there-file-size-limitment-when-using-javascript -filereader-api) –