【发布时间】:2021-11-12 07:08:53
【问题描述】:
我正在使用以下两个函数将 excel 表转换为 json,问题我如何才能将这个 json 文件直接从我的应用程序上传到 Firebase Realtime 数据库?
我想从我的应用程序中添加此功能
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);
}
【问题讨论】:
-
Angular 对此没有什么特别之处。在您的代码中包含 JSON 数据后,您可以将其写入 Firebase,如下所示:firebase.google.com/docs/database/web/read-and-write#write_data 这不适合您吗?或者是加载 JSON 数据的问题,因此与 Firebase 无关。
-
感谢链接帮助我
标签: angular typescript firebase-realtime-database