Javascript 我如何允许公众从我的firebase查看/下载数据?
我正在www.openwifinder.com/上从事一个开放数据项目。我们正在使用firebase存储数据,这些数据将使用javascript加载到ESRI映射。因为我们使用的是开放数据的思想,所以我们希望公众能够下载存储在firebase中的整个点云文件,作为.json文件。我的团队最初从数据页上的“导出数据”按钮复制了该链接,但我们发现该链接是临时的,因为url中的auth令牌即将过期。我们还尝试链接到“Instance.firebaseio.com”url,但当您(在另一个浏览器中)单击它时,它表示您无权查看firebase 理想情况下,我们希望像Firebase一样使用它的开放数据集页面。当你点击那些打开的数据URL时,你会被带到OpenFirebase的数据页面,除了读取和导出数据之外,你什么也做不了。它不一定是公共链接,但即使用户必须注册firebase帐户才能查看数据,也没关系。当然,真正的公共链接对于我们的目的来说是理想的 有什么想法吗?我在网站的帮助页面上搜寻线索Javascript 我如何允许公众从我的firebase查看/下载数据?,javascript,firebase,opendata,Javascript,Firebase,Opendata,我正在www.openwifinder.com/上从事一个开放数据项目。我们正在使用firebase存储数据,这些数据将使用javascript加载到ESRI映射。因为我们使用的是开放数据的思想,所以我们希望公众能够下载存储在firebase中的整个点云文件,作为.json文件。我的团队最初从数据页上的“导出数据”按钮复制了该链接,但我们发现该链接是临时的,因为url中的auth令牌即将过期。我们还尝试链接到“Instance.firebaseio.com”url,但当您(在另一个浏览器中)单击
谢谢 我想不出一种使用API让用户下载数据的方法,所以我使用这里和其他网站上的示例编程实现了这一点。我已经有一段时间没有写下面的代码了(对于一个基于原始OpenWifinder代码的项目来说),所以我不确定它到底应该归于何处,但是我在这里发布了我的解决方案,这样如果其他人发现这个问题,他们就可以工作了。我确信它没有使用API那么优雅,但它可以工作 解决方案可能来自这里,但无法判断此人是否在其他地方得到: Export.js:
//JavaScript文档
var myFirebase=新的Firebase(“FIREBASEURL”);
var-dataJSON;
var jsonDL;
要求([
“dojo/dom”,
“dojo/on”,
“dojo/_base/lang”,“dojo/domReady!”
],功能(
多姆,
在…上
郎,,
多姆雷迪
) {
var exportDataBtn=dom.byId(“exportBtn”);
打开(exportDataBtn,“点击”,JSONToCSVConvertor);
var exportJSONBtn=dom.byId(“dlJSON”);
打开(exportJSONBtn,“单击”,下载JSON);
函数DownloadJSON(){
//log(JSON.stringify(dataJSON));
var jsonString=JSON.stringify(dataJSON,null,4);
//log(jsonString);
jsonDL=导出JSONBTN;
jsonDL.download=“BikeThefts.json”;
jsonDL.href=“data:application/json;charset=utf-8,”+jsonString;
jsonDL.innerHTML=“将数据下载为JSON”;
};
var数据对象;
//此函数获取Firebase中所有数据的“快照”,然后向下导航到“features”子级,然后遍历所有
//“attributes”下的子项,并检索所有属性数据。然后它将它们转换为字符串或数字,并调用addPoint来映射它们
myFirebase.on(“值”,函数(快照){
数据对象=快照;
dataJSON=dataObject.val();
//log(JSON.stringify(dataJSON));
});
函数JSONToCSVConvertor(){
//如果JSONData不是对象,那么JSON.parse将解析对象中的JSON字符串
var arrData=typeof dataJSON!=“object”?JSON.parse(dataJSON):dataJSON;
var CSV=“”;
//在第一行或第一行设置报表标题
var ShowLabel=true;
var ReportTitle=“数据”;
CSV+=ReportTitle+'\r\n\n';
//此条件将生成标签/标题
如果(显示标签){
var行=”;
//此循环将从数组的第一个索引中提取标签
对于(arrData[0]中的var索引){
//现在将每个值转换为字符串和逗号分隔符
行+=索引+',';
}
row=row.slice(0,-1);
//用换行符追加标签行
CSV+=行+'\r\n';
}
//第一个循环是提取每一行
对于(变量i=0;i>window.open(uri);
//但这在某些浏览器中不起作用
//否则您将无法获得正确的文件扩展名
//这个技巧将生成一个临时标记
var link=document.createElement(“a”);
link.href=uri;
//将可见性设置为隐藏,使其不会影响web布局
link.style=“可见性:隐藏”;
link.download=fileName+“.csv”;
//此部件将附加定位标记,并在自动单击后将其删除
document.body.appendChild(链接);
link.click();
document.body.removeChild(link);
}
});
您可以轻松获得URL,任何人都可以使用它通过firebase云功能从firebase获取所需数据:
我想,您也可以通过在子节点url的末尾添加
.json
来打开数据集,例如:。请注意,您需要设置您的读取规则“.read”:true,
,这样公众就可以在无需身份验证的情况下读取您的节点api。哇,这更简单!自从我最初发表这篇文章以来,我就没有使用过firebase,但很高兴知道这一点,以备将来使用。非常感谢。
// 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);
}
});