Upload file ke storage firebase dan simpan data di cloud firestore

gan ane baru belajar firebase , cara upload file ke storage firebase dan simpan data di cloud firestore secara bersamaan bagaimana ya?? sya cari d google belum nemu (d youtube cuma ada storage sama realtime).heeh mohon bantuannya.

file saya kayag gini tp belum lengkap.

var config = {
    apiKey: "AIzaSyDQQRzD6kNZ-7eiwbARm84RdBHY1PAA0To",
     authDomain: "adminar-aa076.firebaseapp.com",
     databaseURL: "https://adminar-aa076.firebaseio.com",
     projectId: "adminar-aa076",
    storageBucket: "adminar-aa076.appspot.com",
    messagingSenderId: "1081522615191"
};
firebase.initializeApp(config);

var storageRef = firebase.storage().ref();

var imagesRef = storageRef.child('images');
function previewFile(){
   var nama = document.getElementById('nama').value;
  var deskripsi = document.getElementById('desc').value;
  var kabupaten = document.getElementById('kab').value;
  var lang = +document.getElementById('lang').value;
  var long = +document.getElementById('long').value;

var file =document.querySelector('input[type=file]').files[0];

var metadata = {
contentType: 'image/jpeg'
};

var uploadTask = storageRef.child('images/' + file.name).put(file, metadata);

uploadTask.on(firebase.storage.TaskEvent.STATE_CHANGED,
function(snapshot) {
    var progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
    console.log('Upload is ' + progress + '% done');
    switch (snapshot.state)
    {
        case firebase.storage.TaskState.PAUSED:
            console.log('Upload is paused');
            break;
        case firebase.storage.TaskState.RUNNING:
            console.log('Upload is running');
            break;
    }
},
function(error) {
    console.log('Upload Error')
},
function() {
    var starsRef = storageRef.child('images/'+ file.name);
    starsRef.getDownloadURL().then(function(url) {
        document.querySelector('#preview').src=url;
        var t=document.querySelector('p')
        t.innerHTML ='<b>Firebase Storage URL: </b>'+url
    }).catch(function(error) {
        console.log('Error Download File');
    });
});
}

karna saya pemula d js dan firebase, saya ga tau peletakan script untuk save data ke firestore dimana, mohon bantuannya

avatar suryadisaputra
@suryadisaputra

19 Kontribusi 0 Poin

Diperbarui 5 tahun yang lalu

1 Jawaban:

  1. pelajari script untuk memasukkan data ke firstore, (saya asumsikan kamu sudah tahu)

masukkanya bisa dibagian sebelum mendapat url storagenya

<pre> document.querySelector('#preview').src=url;

//script masukkan ke firestore disini

var t=document.querySelector('p') t.innerHTML ='&lt;b&gt;Firebase Storage URL: &lt;/b&gt;'+url </pre>

avatar hilmanski
@hilmanski

2689 Kontribusi 2137 Poin

Dipost 5 tahun yang lalu

Login untuk ikut Jawaban