使用angular和firebase获取上传文件的URL“无法设置undefined属性”

时间:2018-05-19 19:56:56

标签: angular typescript firebase file-upload

我正在使用firebase和angular。 我想用这个功能上传文件,它运行得很好但是当我想保存上传到变量 fileUrl

中的文件的URL时出错
ERROR TypeError: Cannot set property 'fileUrl' of undefined

我认为这是解析 fileUrl downloadURL

之间的问题
fileUrl:string;

uploadFile(file: File) {

return new Promise(
  (resolve, reject) => {

    const almostUniqueFileName = Date.now().toString();
    const upload = firebase.storage().ref().child('ProfileImage/' + almostUniqueFileName + "xx").put(file);

      upload.on(firebase.storage.TaskEvent.STATE_CHANGED,
        (snapshot) => {
            console.log('Chargement…');
            this.progress = (upload.snapshot.bytesTransferred / upload.snapshot.totalBytes) * 100;
        },
        (error) => {
          console.log('Erreur de chargement ! : ' + error);
          reject();
        },

      () => {
        upload.snapshot.ref.getDownloadURL().then(function(downloadURL) {

          console.log(downloadURL);

          this.fileUrl = downloadURL;

          console.log(this.fileUrl);

        });

        resolve(upload.snapshot.downloadURL);

      }
    );
  }
);
}
请帮助我

0 个答案:

没有答案