gpt4 book ai didi

javascript - 如何允许公众从我的 Firebase 查看/下载数据?

转载 作者:行者123 更新时间:2023-11-28 00:29:25 25 4
gpt4 key购买 nike

我正在 www.openwifinder.com/上开展一个开放数据项目。我们使用 firebase 来存储我们将使用 javascript 加载到 ESRI map 的数据。因为我们正在研究开放数据的理念,所以我们希望公众能够以 .json 文件的形式下载存储在 firebase 中的整个点云。我的团队最初从数据页面上的“导出数据”按钮复制了链接,但我们发现由于网址中的身份验证 token 过期,该链接是临时的。我们还尝试链接到“Instance.firebaseio.com”网址,但当您单击它(在另一个浏览器中)时,它会提示您无权查看 firebase。

理想情况下,我们希望像 Firebase 的“开放数据集”页面所做的那样。当您单击这些开放数据 URL 时,您将进入开放 Firebase 的数据页面,除了读取和导出数据之外您无法执行任何操作。它不一定是公共(public)链接,但即使用户必须注册 Firebase 帐户才能查看数据也可以。当然,真正的公共(public)链接对于我们的目的来说是理想的选择。

有什么想法吗?我浏览了网站的帮助页面以寻找线索。

谢谢!!

最佳答案

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

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

导出.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);
}

});

关于javascript - 如何允许公众从我的 Firebase 查看/下载数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29150512/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com