所以我一直在尝试制作一些仅将项目上载到firebase的东西。..我无法真正解释该项目,但是我想做的是:
我为此编写的代码是
let downloadURL;
var filename = filenamechosen;
var storageRef = firebase.storage().ref('/dav' + 'projects' + '/' + filename);
var uploadTask = storageRef.put(selectedFile);
uploadTask.on("state_changed", function(snapshot){
}, function(error){
}, function(){
console.log(uploadTask.snapshot.ref.getDownloadURL());
});
};
function uploadOne(){
let projectinf = document.getElementById("projectinfo").value;
let name = document.getElementById("studentname").value;
let cls = document.getElementById("cls").value;
let email = document.getElementById("email").value;
let projectlnk = downloadURL;
let marks = document.getElementById("marks").value;
let submitfrm = document.getElementById("submitfrm");
let studentObj = {
class: cls,
email: email,
projectinfo: projectinf,
projectlink: projectlnk,
marks: "lol",
}
firebase.database().ref('/schools/dav/').child(`${name}`).set(studentObj).then().catch();
console.log("done");
}
要获取下载网址,我得到了这样的答复
jt {a: 0, i: undefined, c: jt, b: null, f: null, …}
a: 2
b: null
c: null
f: null
g: false
h: false
i: "https://firebasestorage.googleapis.com/v0/b/workspace-c7042.appspot.com/o/davproject.... //Continues
我不知道在获取下载网址后如何将其存储在Firebase实时数据库中。
<form action="uploadOne()">
<!--Some inputs as defined in the uploadOne function.-->
<button type="submit">SUBMIT</button>
</form>
这几乎是我正在使用的所有表格。
答案 0 :(得分:1)
getDownloadURL()
方法是异步,并返回一个Promise,该URL可以使用downloadURL进行解析。
因此,您需要按照以下步骤做一些事情,使用then()
方法来等待Promise解决:
uploadTask.snapshot.ref.getDownloadURL().then(function(downloadURL) {
console.log('File available at', downloadURL);
});
您的代码中如何链接两个不同的部分(哪个调用哪个以及如何选择文件)尚不清楚,但是下面显示了一种可能性:
let downloadURL;
var filename = filenamechosen;
var storageRef = firebase.storage().ref('/dav' + 'projects' + '/' + filename);
var uploadTask = storageRef.put(selectedFile);
uploadTask.on("state_changed",
null, /// <- See https://firebase.google.com/docs/reference/js/firebase.storage.UploadTask#on
null,
function(){
// Upload completed successfully, now we can get the download URL
uploadTask.snapshot.ref.getDownloadURL()
.then(function(downloadURL) {
console.log('File available at', downloadURL);
uploadOne(downloadURL); // <- We call the uploadOne function passing the downloadURL as parameter
});
});
function uploadOne(downloadURL){
let projectinf = document.getElementById("projectinfo").value;
let name = document.getElementById("studentname").value;
let cls = document.getElementById("cls").value;
let email = document.getElementById("email").value;
let projectlnk = downloadURL;
let marks = document.getElementById("marks").value;
let submitfrm = document.getElementById("submitfrm");
let studentObj = {
class: cls,
email: email,
projectinfo: projectinf,
projectlink: projectlnk,
marks: "lol",
}
firebase.database().ref('/schools/dav/').child(`${name}`).set(studentObj)
.then(function() {
console.log("done");
})
.catch(function(error) {
console.log(error);
});
}
这样,uploadTask
触发了对数据库的写入。您可以根据需要进行调整,但是在任何情况下,您只能在实现Promise时调用的回调函数中获得downloadURL
的值。