Issue
I am using two functions below to convert excel sheet to json, Question How can I then upload this json file to Firebase Realtime database direct from my app ?
I want to add this function from my app
onFileChange(ev) {
let workBook = null;
let jsonData = null;
const reader = new FileReader();
const file = ev.target.files[0];
reader.onload = (event) => {
const data = reader.result;
workBook = XLSX.read(data, { type: 'binary' });
jsonData = workBook.SheetNames.reduce((initial, name) => {
const sheet = workBook.Sheets[name];
initial[name] = XLSX.utils.sheet_to_json(sheet);
return initial;
}, {});
const dataString = JSON.stringify(jsonData);
document.getElementById('output').innerHTML = dataString.slice(0, 300).concat('...');
this.setDownload(dataString);
};
reader.readAsBinaryString(file);
}
setDownload(data) {
this.willDownload = true;
setTimeout(() => {
const el = document.querySelector('#download');
el.setAttribute('href', `data:text/json;charset=utf-8,${encodeURIComponent(data)}`);
el.setAttribute('download', 'xlsxtojson.json');
}, 1000);
}
Solution
As stated by Frank, there is nothing specific to Angular as you can upload your JSON to RTDB by following the documentation.
However, when working with Angular you'll want to use the angular/fire package which is the official library for Angular and Firebase development.
In your constructor, you'll import the AngularFireDatabase class
constructor(private db: AngularFireDatabase) {}
And then here is your function that will upload the data:
async uploadJSONToRTDB(yourDataHere: any): Promise<void> {
// reference the node
const ref = this.db.database.ref('ref/to/your/node');
await ref.set(yourDataHere);
}
Answered By - Hydra

0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.