mirror of
https://github.com/danielsogl/awesome-cordova-plugins.git
synced 2025-01-31 18:49:43 +08:00
Added implementation for readAsText() method.
This commit is contained in:
parent
493e6552d7
commit
96c47df151
@ -462,7 +462,57 @@ export class File {
|
||||
|
||||
// static writeExistingFile(path: string, fileName: string, text: string): Promise<any> { return }
|
||||
|
||||
// static readAsText(path: string, file: string): Promise<any> { return }
|
||||
/**
|
||||
* Read a file as string.
|
||||
*
|
||||
* @param {string} path Base FileSystem. Please refer to the iOS and Android filesystems above
|
||||
* @param {string} fileName Name of file to move
|
||||
* @return Returns a Promise that resolves or rejects with an error.
|
||||
*/
|
||||
static readAsText(path: string, fileName: string): Promise<any> {
|
||||
let resolveFn, rejectFn;
|
||||
let promise = new Promise((resolve, reject) => {resolveFn = resolve; rejectFn = reject; });
|
||||
|
||||
if ((/^\//.test(fileName))) {
|
||||
rejectFn('file-name cannot start with \/');
|
||||
}
|
||||
|
||||
try {
|
||||
window.resolveLocalFileSystemURL(path, function (fileSystem) {
|
||||
fileSystem.getFile(fileName, {create: false}, function (fileEntry) {
|
||||
fileEntry.file(function (file) {
|
||||
var reader = new FileReader();
|
||||
|
||||
reader.onloadend = function(e) {
|
||||
if (this.result !== undefined && this.result !== null) {
|
||||
resolveFn(this.result);
|
||||
} else if (this.error !== undefined && this.error !== null) {
|
||||
rejectFn(this.error);
|
||||
} else {
|
||||
rejectFn({code: null, message: 'READER_ONLOADEND_ERR'});
|
||||
}
|
||||
}
|
||||
|
||||
reader.readAsText(file);
|
||||
}, function (error) {
|
||||
error.message = File.cordovaFileError[error.code];
|
||||
rejectFn(error);
|
||||
});
|
||||
}, function (err) {
|
||||
err.message = File.cordovaFileError[err.code];
|
||||
rejectFn(err);
|
||||
});
|
||||
}, function (er) {
|
||||
er.message = File.cordovaFileError[er.code];
|
||||
rejectFn(er);
|
||||
});
|
||||
} catch (e) {
|
||||
e.message = File.cordovaFileError[e.code];
|
||||
rejectFn(e);
|
||||
}
|
||||
|
||||
return promise;
|
||||
}
|
||||
|
||||
// static readAsDataURL(path: string, file: string): Promise<any> { return }
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user