简体   繁体   English

Firebase - 下载 URL(Firebase 存储)并将其保存在 Firebase 实时数据库中

[英]Firebase - getting the download URL (Firebase Storage) and saving it in Firebase realtime database

I have been trying to make something which just uploads a project to Firebase and all.. I cant really explain the project but what I am trying to do is:我一直在尝试做一些东西,只是将项目上传到 Firebase 等等。我不能真正解释这个项目,但我想做的是:

  1. Get the Firebase download URL when uploading the file.上传文件时获取Firebase下载URL。
  2. Save it in the Firebase realtime database.将其保存在 Firebase 实时数据库中。

The code I have made for it is:我为它编写的代码是:

    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");
}

For getting the download url, I am getting something like this as a response:为了获得下载 url,我得到了这样的回应:

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

I have no idea how I can store it in the Firebase realtime database after getting the download URL:在下载 URL 后,我不知道如何将其存储在 Firebase 实时数据库中:

<form action="uploadOne()">
<!--Some inputs as defined in the uploadOne function.-->
<button type="submit">SUBMIT</button>
</form>

This is almost the whole form I am using with that thing.这几乎是我在那个东西上使用的整个形式。

The getDownloadURL() method is asynchronous and returns a Promise which resolves with the downloadURL. getDownloadURL()方法是异步的,它返回一个 Promise ,它使用 downloadURL 解析。

You therefore need to do something along these lines, using the then() method to wait that the Promise resolves:因此,您需要执行以下操作,使用then()方法等待 Promise 解析:

  uploadTask.snapshot.ref.getDownloadURL().then(function(downloadURL) {
    console.log('File available at', downloadURL);
  });

It is not clear in your code how the two different parts are linked (which one calls which and how the file is selected), but one possibility is shown below:您的代码中不清楚这两个不同部分是如何链接的(哪一个调用哪个以及如何选择文件),但一种可能性如下所示:

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);
      });
}

This way it is the uploadTask that triggers the write to the database.这样,触发写入数据库的是uploadTask You can adapt as necessary but in any case you will only get the value of the downloadURL in the callback function called when the Promise is fulfilled.您可以根据需要进行调整,但无论如何您只会在 Promise 完成时调用的回调 function 中获得downloadURL的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM