我编写了一个函数将本地文件(使用cordova // upload trip data to firebase. currently a public bucket
cloudUpload(prg) {
console.log ("cloud upload");
//this.presentLoader("loading...");
let storageRef = firebase.storage().ref();
console.log ("storage ref is "+storageRef);
this.file.readAsArrayBuffer(this.file.dataDirectory, this.logFile)
.then (succ=>{
console.log ("File read");
console.log (succ);
let blob = new Blob([succ],{type:"text/plain"});
console.log ("Blob created");
let name = "file-"+Date()+".txt";
let uploadUrl = storageRef.child(`tripdata/${name}`);
let uploadTask = uploadUrl.put(blob);
uploadTask.on(firebase.storage.TaskEvent.STATE_CHANGED,
(snapshot) => {
let progress = Math.round((snapshot.bytesTransferred / snapshot.totalBytes) * 100);
prg.val = progress;
},
(error) => {
console.log ("Firebase put error "+error);
setTimeout(()=>{prg.val = -1; },500);
this.presentToast("upload error","error") },
() => { prg.val = 100;
setTimeout(()=>{prg.val = -1; },500);
// write download URL to realtime DB so we can iter it later
// there is no API in storage today to iterate
let downloadURL = uploadTask.snapshot.downloadURL;
console.log ("Download url is "+downloadURL);
let key = 'tripDataIndex/'+name;
console.log ("key="+key);
firebase.database().ref(key)
.set ({'url':downloadURL, 'uploadedon':Date()}); // nothing created
.catch (err=> {console.log ("ERROR "+err);this.presentToast("error creating index","error")})
this.presentToast("upload complete")}
)
})
.catch (err=>{console.log ("Cordova Read Error "+err);})
}
)上传到我的firebase存储。看起来没有API来迭代存储中的所有文件(基于其他SO线程),所以我决定将下载位置写入我的实时数据库。
目前,我的实时数据库和存储中的身份验证 OFF 用于测试。文件上传正确,我可以在我的存储中看到它,但我没有看到任何数据库条目。有人可以帮助解决出错的问题吗?
TestInitialize
答案 0 :(得分:0)
似乎问题在于关键价值。我的名字"是
let name = "file-"+Date()+".txt";
Date()
包含与naming rules冲突的空格/括号等 - 使用不同的键名称完美运行!
然而,奇怪的是它没有抛出错误。我在最后添加了一个.catch
,就像Frank建议的那样,但它从未进入catch处理程序。