Как получить URL-адрес загрузки изображения из события хранилища firebase state_changed? - PullRequest
0 голосов
/ 25 мая 2018

Этот код должен работать, но независимо от того, что я делаю, я не могу получить downloadURL.Я использую Vue JS.Это код для загрузки файла.

А ниже вы можете увидеть изображение, которое показывает журнал консоли.Он успешно сохранил изображение, единственная проблема в том, что я не получаю downloadURL, похоже, его нет.

// upload file
    uploadFile(file, metadata) {
            if(file === null) return false

            let pathToUpload = this.currentChannel.id
            // parent means Messages.vue getMessagesRef() that returns either public or private channel
            let ref = this.$parent.getMessagesRef()
            // getPath() refers to the path below
            let filePath = this.getPath() + '/' + uuidV4() + '.jpg'

            // upload file
            this.uploadTask = this.storageRef.child(filePath).put(file, metadata)
            // upload state
            this.uploadState = "uploading"


            // on upload state change
            this.uploadTask.on('state_changed', snapshot => {
                console.log('image uploaded/state_changed in storage: ', snapshot)
                // Upload en cours
                let percent = (snapshot.bytesTransferred / snapshot.totalBytes) * 100
                // $("#uploadedFile").progress("set percent", percent)
                $(".progress-bar").css("width", percent+'%')
            }, error => {
                // Error
                this.errors.push(error.message)
                this.uploadState = 'error'
                this.uploadTask = null
            }, () => {
                // Upload finished
                this.uploadState = 'done'
                console.log('done upload')
                // reset form
                this.$refs.file_modal.resetForm()
                // recover the url of file
                let fileUrl = this.uploadTask.snapshot.downloadURL
                console.log('downloadURL(snapshot) from firebase: ', this.uploadTask.snapshot)

                // sendFileMessage() will pass file as parameter on upload
                this.sendFileMessage(fileUrl, ref, pathToUpload)
            })
    },

Результат Console.log ():

enter image description here

Спасибо за помощь!

1 Ответ

0 голосов
/ 25 мая 2018

Согласно документу пожарной базы , вы должны позвонить uploadTask.snapshot.ref.getDownloadURL в завершенном обратном вызове

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