使用JavaScript从Firebase下载为JSON

asi*_*rta 5 javascript json firebase firebase-realtime-database

我想从JavaScript导出Firebase对象作为JSON并下载它.例如,此文件是在patients/参考.我想在这个格式的.json文件中下载它:

"-LCZPCkiGCNhFaQ8ckJ-" : {
  "altura" : 165,
  "apellido" : "Salas",
  "extra" : {
    "Jubilado" : "No",
    "Localidad" : "Madrid",
    "Telefono" : "698532147"
  },
  "fechaNacimiento" : "14/10/1961",
  "nombre" : "Paquita",
  "sexo" : "Mujer"
}
Run Code Online (Sandbox Code Playgroud)

我只能下载存储在存储中但不存储在实时数据库中的文件

 firebase.storage().ref('grabaciones/').child(grabacion).getDownloadURL().then(function (url) {
  let a = document.createElement("a");
  a.download = grabacion;
  a.href = url;
  document.body.appendChild(a);
  a.click();
}).catch(function (error) {
  // Handle any errors
  console.log(error);
});
Run Code Online (Sandbox Code Playgroud)

先感谢您.

更新的代码,其中元素是作为JSON获取的,并以.json的形式下载.仅适用于Firefox:

$scope.exportarJSON = function (paciente) {
        console.log(grabacion);
        firebase.database().ref('pacientes/').child(pacinte).once('value', function (paciente) {
          download(paciente + ".json", JSON.stringify(paciente.val()));
        });
    };



 function download(filename, text) {
    var element = document.createElement('a');
    element.setAttribute('href', 'data:text/json;charset=utf-8,' + encodeURIComponent(text));
    element.setAttribute('download', filename);

    element.style.display = 'none';
    document.body.appendChild(element);

    element.click();

    document.body.removeChild(element);
  }
Run Code Online (Sandbox Code Playgroud)

Fra*_*len 6

Firebase数据库中的一段数据都有自己唯一的URL.例如,我快速将您的示例数据导入我的一个数据库,现在它仍然存在https://stackoverflow.firebaseio.com/50408054/-LCZPCkiGCNhFaQ8ckJ-.

如果您转到该URL,系统将提示您登录.由于您无权访问我的项目,因此无法看到我的数据库控制台.

但您可以将数据作为JSON访问.由于我已授予对数据的公共读取权限,因此您可以通过以下URL将其作为JSON访问:https://stackoverflow.firebaseio.com/50408054/-LCZPCkiGCNhFaQ8ckJ-.json.正如您所看到的,这是与以前相同的URL,但现在.json添加在最后.

这是Firebase的REST API的一部分,在此完整记录:https://firebase.google.com/docs/database/rest.只要数据是公开可读的,您就应该能够在下载链接中使用此REST URL.

  • 我相信链接已更改为:https://firebase.google.com/docs/database/rest/start (2认同)

asi*_*rta 0

最后,我成功地通过从 Firebase 获取数据并将 JSON.stringify () 应用于该数据来做到这一点。后来为了将它们下载为 .json 文件,我创建了一个 Blob,其中包含此问题代码后面的字符串。感谢您的回答,他们很有启发

 $scope.exportarJSON = function (paciente) {
        console.log(grabacion);
        firebase.database().ref('pacientes/').child(pacinte).once('value', function (paciente) {
          download(paciente + ".json", paciente.val());
        });
    };


function download(filename, paciente) {
    let file = JSON.stringify(paciente);
    let blob = new Blob([file], {type: "application/json"});
    let url  = URL.createObjectURL(blob);
    let element = document.createElement('a');
    element.setAttribute('href', url);
    element.setAttribute('download', paciente.nombre + ".json");

    element.style.display = 'none';
    document.body.appendChild(element);

    element.click();

    document.body.removeChild(element);
  }
Run Code Online (Sandbox Code Playgroud)