如何允许公众查看/下载我的firebase中的数据?

问题描述 投票:1回答:2

我正在www.openwifinder.com/上开展一个开放数据项目。我们使用firebase来存储我们将使用javascript加载到ESRI地图的数据。因为我们正在使用开放数据的想法,我们希望公众能够将存储在firebase中的整个点云下载为.json文件。我的团队最初从数据页面上的“导出数据”按钮复制了链接,但我们发现由于URL中的auth令牌过期,该链接是临时的。我们还尝试链接到“Instance.firebaseio.com”网址,但是当您点击它(在另一个浏览器中)时,它表示您无权查看firebase。

理想情况下,我们希望像Firebase一样使用它的开放数据集页面。当您单击这些打开的数据URL时,您将进入打开的firebase的数据页面,除了读取和导出数据之外,您无法执行任何操作。它不一定是公共链接,但即使用户必须注册firebase帐户来查看数据,它也没关系。当然,真正的公共链接对我们来说是理想的。

有任何想法吗?我已经浏览了网站的帮助页面以寻找线索。

谢谢!!

javascript firebase opendata
2个回答
1
投票

我无法找到使用API​​让用户下载数据的方法,因此我使用此处和网络上的其他地方的示例以编程方式进行编程。自从我编写以下代码(以及基于原始OpenWifinder代码的项目)已经有一段时间了,所以我不确定将其归于何处,但是我在这里发布我的解决方案,以便其他人找到这个他们可以解决这个问题。我确信它不如使用API​​那么优雅,但它有效。

解决方案可能来自这里,但无法判断这个人是否在其他地方得到它:https://gist.github.com/AnirbanKundu/26f2ba229174b1915873

Export.js:

// JavaScript Document
var myFirebase = new Firebase("FIREBASEURL");
var dataJSON;
var jsonDL;
require([
      "dojo/dom", 
      "dojo/on", 
      "dojo/_base/lang", "dojo/domReady!"
    ], function(
    dom, 
    on, 
    lang,
    domReady
    )  {
 var exportDataBtn = dom.byId("exportBtn");
      on(exportDataBtn, "click", JSONToCSVConvertor);
var exportJSONBtn = dom.byId("dlJSON");
      on(exportJSONBtn, "click", DownloadJSON);
function DownloadJSON(){
    //console.log(JSON.stringify(dataJSON));
    var jsonString = JSON.stringify(dataJSON, null, 4);
    //console.log(jsonString);
  jsonDL = exportJSONBtn;
  jsonDL.download = "BikeThefts.json";
  jsonDL.href = "data:application/json;charset=utf-8," + jsonString;
  jsonDL.innerHTML = "Download data as JSON";
};
var dataObject;
    //this function grabs a 'snapshot' of all the data in Firebase, then navigates down to the 'features' child. It then iterates through all the
    //children under 'attributes' and retrieves all attribute data. Then it converts them to strings or numbers and calls addPoint to map them
myFirebase.on("value", function(snapshot) {
dataObject = snapshot;
dataJSON = dataObject.val();
//console.log(JSON.stringify(dataJSON));

});

function JSONToCSVConvertor() {
    //If JSONData is not an object then JSON.parse will parse the JSON string in an Object
    var arrData = typeof dataJSON != 'object' ? JSON.parse(dataJSON) : dataJSON;

    var CSV = '';    
    //Set Report title in first row or line
    var ShowLabel = true;
    var ReportTitle = "Data";
    CSV += ReportTitle + '\r\n\n';

    //This condition will generate the Label/Header
    if (ShowLabel) {
        var row = "";

        //This loop will extract the label from 1st index of on array
        for (var index in arrData[0]) {

            //Now convert each value to string and comma-seprated
            row += index + ',';
        }

        row = row.slice(0, -1);

        //append Label row with line break
        CSV += row + '\r\n';
    }

    //1st loop is to extract each row
    for (var i = 0; i < arrData.length; i++) {
        var row = "";

        //2nd loop will extract each column and convert it in string comma-seprated
        for (var index in arrData[i]) {
            row += '"' + arrData[i][index] + '",';
        }

        row.slice(0, row.length - 1);

        //add a line break after each row
        CSV += row + '\r\n';
    }

    if (CSV == '') {        
        alert("Invalid data");
        return;
    }   

    //Generate a file name
    var fileName = "BikeTheftIncidents_";
    //this will remove the blank-spaces from the title and replace it with an underscore
    fileName += ReportTitle.replace(/ /g,"_");   

    //Initialize file format you want csv or xls
    var uri = 'data:text/csv;charset=utf-8,' + escape(CSV);

    // Now the little tricky part.
    // you can use either>> window.open(uri);
    // but this will not work in some browsers
    // or you will not get the correct file extension    

    //this trick will generate a temp <a /> tag
    var link = document.createElement("a");    
    link.href = uri;

    //set the visibility hidden so it will not effect on your web-layout
    link.style = "visibility:hidden";
    link.download = fileName + ".csv";

    //this part will append the anchor tag and remove it after automatic click
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
}

});

0
投票

您可以轻松拥有一个URL,任何人都可以使用它通过Firebase云功能从firebase获取您想要的数据:

https://medium.com/@dalenguyen/csv-or-json-export-with-firebase-functions-fa4a4b1416aa

© www.soinside.com 2019 - 2024. All rights reserved.