Adding the plugin to your project
To install the plugin, move XhrFileReader.js to your project's www folder and include a reference to it in your html files.
Using the plugin
To instantiate a new reader use the folling code:
var reader = cordova.require("cordova/plugin/xhrfilereader");Setup your event handlers:
// called once the reader beginsUnfortunately, you will only get an error on files read over HTTP. When you specify a file:// path the request status is always 0. There is no way to tell between a successful read or an error. So if you specify a file that does not exist like "file:///does.not.exist.txt" you will get an empty evt.target.result in your onloadend handler.
reader.onloadstart = function() {
console.log("load start");
};
// called when the file has been completely read
reader.onloadend = function(evt) {
console.log("File read");
console.log(evt.target.result);
};
// called if the reader encounters an errorProgress events are fired but are not very useful as they don't contain partial results.
reader.onerror = function(error) {
console.log("Error: " + error.code);
};
// called while the file is being readFinally call your read method, for instance:
reader.onprogress = function(evt) {
console.log("progress");
};
reader.readAsText("http://www.google.com"); reader.readAsText("file:///android_asset/www/config.json"); reader.readAsText("file:///sdcard/error.log");and that's about it. Obviously, this plugin is most useful when you need to read a text file from the assets folder.
6 comments:
Err... respectfully... why? Why not just $.get?
@Raymond Camden
Well....
1) That assumes you are using jQuery.
2) It doesn't follow the W3C FileReader spec.
but there is no reason why you couldn't use your own XHR (jQuery, Dojo, Sencha) to do exactly the same thing I did.
This is also a stealth feature for Cordova. If people like it I'll merge the changes into the FileReader code so that it will be able to handle files in the assets as well.
1) Heh, well, it works in Zepto too. ;)
2) Well, ok.
"This is also a stealth feature for Cordova. If people like it I'll merge the changes into the FileReader code so that it will be able to handle files in the assets as well."
Fair enough. ColdFusion supports this as well.
Hello everybody,
I think this is a great improvement to the FileReader API so i think it should be integrated in the cordova code.
I agree with Raymond when he tells that a simple $.get call (or whatever the framework you use gives) is simple and effective, but neverthelessI think this is very useful especially for reading files in the asset directory. With this plugin the user will be able to read filesystem and asset files using the same API and this will result in a simpler coding.
What do you think?
i try to install , but fail.
could you please tell me how to install with example.
thanks.
@Farid Hidayat
What's to install? You just need to use the .js file in your project.
Post a Comment