Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
152 views
in Technique[技术] by (71.8m points)

javascript - Firebase存储文件名更改(firebase storage file name change)

I am a newbie to firebase and Javascript.

(我是Firebase和Javascript的新手。)
How do I change the file name to user's uid and save it to storage?

(如何将文件名更改为用户的uid并将其保存到存储中?)

And I want to upload a file after successful registration.

(我想在成功注册后上传文件。)

Should I write a function in createUserWithEmailAndPassword or onAuthStateChanged?

(我应该在createUserWithEmailAndPassword或onAuthStateChanged中编写函数吗?)

function handleFileSelect(evt) {
    evt.stopPropagation();
    evt.preventDefault();
    var file = evt.target.files[0];

    var metadata = {
      'contentType': file.type
    };

    var storage_ref = storage.ref();

    storage_ref.child('userImages/' + user.uid).put(file, metadata).then(function(snapshot) {

      snapshot.ref.getDownloadURL().then(function(url) {
        console.log('File available at', url);
        profile_url = url;

      });
    }).catch(function(error) {

      console.error('Upload failed:', error);

    });



  } // handleFileSelect

  document.getElementById('input_img').addEventListener('change',handleFileSelect, false);
  ask by Li Optt translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can't change the file name directly.

(您不能直接更改文件名。)

But there is an indirect way to do that, check this question

(但是有一种间接的方法,请检查此问题)

You want User's UID as file name but you won't get that in createUserWithEmailAndPassword so you need to add that logic into onAuthStateChanged method.

(您希望将用户的UID作为文件名,但不会在createUserWithEmailAndPassword得到它,因此您需要将该逻辑添加到onAuthStateChanged方法中。)

But onAuthStateChanged get called every time whenever users login so you need to build some kind of logic to execute your file upload method only first time when user log in.

(但是每次用户登录时都会调用onAuthStateChanged因此您需要构建某种逻辑以仅在用户登录时才执行文件上传方法。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...