1
votes

I want to let users upload profile pictures and I do this by using html file API.

Below is my code for uploading and showing the profile picture:

uploadPic.addEventListener('change',function(evt){
    var file = this.files[0];
    firebase.storage().ref().child(user_uid + "/" + file.name).put(file).then(function(){
        console.log("uploaded to storage");
    }).catch(function(e){
        console.log(e.message);
    });

    firebase.storage().ref().child(user_uid + "/" + file.name).getDownloadURL().then(function(url){
        user.updateProfile({photoURL: url}).then(function(){
            console.log("change success");
        }).catch(function(e){
            console.log(e.message);
        });
    }).catch(function(e){
        console.log(e.message);
    });
});

/*profile pic*/
if(user.photoURL===null){
    profilePic.src = "image/anonymous_big.png";
}else{
    console.log(user.photoURL);
    profilePic.src = user.photoURL;
}


Whenever the user changes the file it will upload to firebase storage then downloaded as URL and give the value to user.photoURL.

The picture was uploaded and downloaded successfully, however the profile picture looks like this:
the profile picture

If I console.log(user.photoURL) and click the url in console it shows the correct picture. I have no idea what went wrong. Thanks in advance.

1
what do you exactly get with console.log(user.photoURL);?Renaud Tarnec
Are you sure that the picture is already uploaded when you call firebase.storage().ref().child(user_uid + "/" + file.name).getDownloadURL()? Because the first put is returning a promise and you shoudl proaly chain the promises.Renaud Tarnec
It does show "uploaded to storage" then "change success", but let me try that.WhaleShark
If it says "permission denied", are you sure that you have correctly configured the Storage access rules?Renaud Tarnec

1 Answers

1
votes

Based on your comment detailing the error "permission denied", you should double check your Storage access rules.