Chapter 34 Files
HI,
i follows the filereader API practice from page 304 to page 307, but my code does not seem to work, the error message is "Uncaught TypeError: this.store is not a function"
the following is the code i entered:
$(screen).find('#importJSONFile').change(function( evt) {
var reader = new FileReader();
reader.onload = function(evt) {
var contacts = JSON.parse(evt.target.result);
for (var i = 0; i < contacts.length; i++) {
this.store(contacts[i]);
}
location.reload();
}.bind(this);
reader.readAsText(evt.target.files[0]);
}.bind(this));
and i think this is my store function:
function store(contact) {
var contactsStored = localStorage.getItem('contacts');
var contacts = [];
if (contactsStored) {
contacts = JSON.parse(contactsStored);
}
contacts.push(contact);
localStorage.setItem('contacts', JSON.stringify(contacts));
}
Please let me know how to rectify this.
Many thanks in advance
|