@angular/fire AngularFireStorage getDownloadUrl()












1















I am trying to use the new @angular/fire 5.1.0
to view an image uploaded to firebase using AngularFireStorage.
I used to be able to make use of task.downloadURL() in angularfire2



"correct me if I'm wrong but now I have to use afStorage.getDownloadURL()
Please can you kindly assist me with setting imageUrl correctly.



import { AngularFireStorage, AngularFireStorageReference, AngularFireUploadTask } from '@angular/fire/storage'
....
downloadURL: Observable<string>;
imageUrl: string;
....

async onGetFromGallery(){
try{ ....
const imageData = await this.camera.getPicture(options);
const image = 'data:image/jpeg;base64,' + imageData;
const id = Math.random().toString(36).substring(2);
const user = this.authenticatorService.getUser();
this.ref = this.afStorage.ref(user.uid + '/images/categories/'+id);
this.task = this.ref.putString(image, 'data_url');

//--- Previously:angularfire2:
// this.downloadURL = this.ref.getDownloadURL();
// this.downloadURL.subscribe(url=> this.imageUrl=url);
//--- now replaced by this.ref.getDownloadURL()...

//My Attempt - unable to getDownloadUrl?
this.task
.snapshotChanges().toPromise().then(_ =>
{
this.ref.getDownloadURL().toPromise().then(res => {
console.log('URL: ', res);
this.imageUrl = res;
});
})

} catch(e) {
console.error(e);
this.errorMessage = JSON.stringify(e);
}
}


view excerpt:



<img [src]="imageUrl"  style="width:100%;">


package.json excerpt:




"@angular/compiler-cli": "5.2.11",

"@angular/fire": "^5.1.0",

"firebase": "^5.5.9",

"cordova-android": "~7.0.0",




Thank you.










share|improve this question




















  • 1





    According to github.com/angular/angularfire2/blob/master/docs/storage/… you should only need this.imageUrl = ref.getDownloadURL().

    – Frank van Puffelen
    Nov 26 '18 at 15:15
















1















I am trying to use the new @angular/fire 5.1.0
to view an image uploaded to firebase using AngularFireStorage.
I used to be able to make use of task.downloadURL() in angularfire2



"correct me if I'm wrong but now I have to use afStorage.getDownloadURL()
Please can you kindly assist me with setting imageUrl correctly.



import { AngularFireStorage, AngularFireStorageReference, AngularFireUploadTask } from '@angular/fire/storage'
....
downloadURL: Observable<string>;
imageUrl: string;
....

async onGetFromGallery(){
try{ ....
const imageData = await this.camera.getPicture(options);
const image = 'data:image/jpeg;base64,' + imageData;
const id = Math.random().toString(36).substring(2);
const user = this.authenticatorService.getUser();
this.ref = this.afStorage.ref(user.uid + '/images/categories/'+id);
this.task = this.ref.putString(image, 'data_url');

//--- Previously:angularfire2:
// this.downloadURL = this.ref.getDownloadURL();
// this.downloadURL.subscribe(url=> this.imageUrl=url);
//--- now replaced by this.ref.getDownloadURL()...

//My Attempt - unable to getDownloadUrl?
this.task
.snapshotChanges().toPromise().then(_ =>
{
this.ref.getDownloadURL().toPromise().then(res => {
console.log('URL: ', res);
this.imageUrl = res;
});
})

} catch(e) {
console.error(e);
this.errorMessage = JSON.stringify(e);
}
}


view excerpt:



<img [src]="imageUrl"  style="width:100%;">


package.json excerpt:




"@angular/compiler-cli": "5.2.11",

"@angular/fire": "^5.1.0",

"firebase": "^5.5.9",

"cordova-android": "~7.0.0",




Thank you.










share|improve this question




















  • 1





    According to github.com/angular/angularfire2/blob/master/docs/storage/… you should only need this.imageUrl = ref.getDownloadURL().

    – Frank van Puffelen
    Nov 26 '18 at 15:15














1












1








1








I am trying to use the new @angular/fire 5.1.0
to view an image uploaded to firebase using AngularFireStorage.
I used to be able to make use of task.downloadURL() in angularfire2



"correct me if I'm wrong but now I have to use afStorage.getDownloadURL()
Please can you kindly assist me with setting imageUrl correctly.



import { AngularFireStorage, AngularFireStorageReference, AngularFireUploadTask } from '@angular/fire/storage'
....
downloadURL: Observable<string>;
imageUrl: string;
....

async onGetFromGallery(){
try{ ....
const imageData = await this.camera.getPicture(options);
const image = 'data:image/jpeg;base64,' + imageData;
const id = Math.random().toString(36).substring(2);
const user = this.authenticatorService.getUser();
this.ref = this.afStorage.ref(user.uid + '/images/categories/'+id);
this.task = this.ref.putString(image, 'data_url');

//--- Previously:angularfire2:
// this.downloadURL = this.ref.getDownloadURL();
// this.downloadURL.subscribe(url=> this.imageUrl=url);
//--- now replaced by this.ref.getDownloadURL()...

//My Attempt - unable to getDownloadUrl?
this.task
.snapshotChanges().toPromise().then(_ =>
{
this.ref.getDownloadURL().toPromise().then(res => {
console.log('URL: ', res);
this.imageUrl = res;
});
})

} catch(e) {
console.error(e);
this.errorMessage = JSON.stringify(e);
}
}


view excerpt:



<img [src]="imageUrl"  style="width:100%;">


package.json excerpt:




"@angular/compiler-cli": "5.2.11",

"@angular/fire": "^5.1.0",

"firebase": "^5.5.9",

"cordova-android": "~7.0.0",




Thank you.










share|improve this question
















I am trying to use the new @angular/fire 5.1.0
to view an image uploaded to firebase using AngularFireStorage.
I used to be able to make use of task.downloadURL() in angularfire2



"correct me if I'm wrong but now I have to use afStorage.getDownloadURL()
Please can you kindly assist me with setting imageUrl correctly.



import { AngularFireStorage, AngularFireStorageReference, AngularFireUploadTask } from '@angular/fire/storage'
....
downloadURL: Observable<string>;
imageUrl: string;
....

async onGetFromGallery(){
try{ ....
const imageData = await this.camera.getPicture(options);
const image = 'data:image/jpeg;base64,' + imageData;
const id = Math.random().toString(36).substring(2);
const user = this.authenticatorService.getUser();
this.ref = this.afStorage.ref(user.uid + '/images/categories/'+id);
this.task = this.ref.putString(image, 'data_url');

//--- Previously:angularfire2:
// this.downloadURL = this.ref.getDownloadURL();
// this.downloadURL.subscribe(url=> this.imageUrl=url);
//--- now replaced by this.ref.getDownloadURL()...

//My Attempt - unable to getDownloadUrl?
this.task
.snapshotChanges().toPromise().then(_ =>
{
this.ref.getDownloadURL().toPromise().then(res => {
console.log('URL: ', res);
this.imageUrl = res;
});
})

} catch(e) {
console.error(e);
this.errorMessage = JSON.stringify(e);
}
}


view excerpt:



<img [src]="imageUrl"  style="width:100%;">


package.json excerpt:




"@angular/compiler-cli": "5.2.11",

"@angular/fire": "^5.1.0",

"firebase": "^5.5.9",

"cordova-android": "~7.0.0",




Thank you.







javascript firebase firebase-storage angularfire2






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 15:11









Frank van Puffelen

244k29387415




244k29387415










asked Nov 26 '18 at 11:36









altergothenaltergothen

10317




10317








  • 1





    According to github.com/angular/angularfire2/blob/master/docs/storage/… you should only need this.imageUrl = ref.getDownloadURL().

    – Frank van Puffelen
    Nov 26 '18 at 15:15














  • 1





    According to github.com/angular/angularfire2/blob/master/docs/storage/… you should only need this.imageUrl = ref.getDownloadURL().

    – Frank van Puffelen
    Nov 26 '18 at 15:15








1




1





According to github.com/angular/angularfire2/blob/master/docs/storage/… you should only need this.imageUrl = ref.getDownloadURL().

– Frank van Puffelen
Nov 26 '18 at 15:15





According to github.com/angular/angularfire2/blob/master/docs/storage/… you should only need this.imageUrl = ref.getDownloadURL().

– Frank van Puffelen
Nov 26 '18 at 15:15












1 Answer
1






active

oldest

votes


















3














There are some other minor structure changes in the code too, not just renaming the method.



Check out the example code in the official AngularFire2 docs under the section titled "Monitoring upload percentage". Specifically, the example upload function they include:



uploadFile(event) {
const file = event.target.files[0];
const filePath = 'name-your-file-path-here';
const fileRef = this.storage.ref(filePath);
const task = this.storage.upload(filePath, file);

// observe percentage changes
this.uploadPercent = task.percentageChanges();

// get notified when the download URL is available
task.snapshotChanges().pipe(
finalize(() => this.downloadURL = fileRef.getDownloadURL() )
)
.subscribe()
}


and even more specifically, this part of that function, the listener....



// get notified when the download URL is available
task.snapshotChanges().pipe(
finalize(() => this.imageUrl = fileRef.getDownloadURL() )
)
.subscribe()


It will fire when the upload has finalized and populate the this.downloadURL variable with the URL. You've already defined the fileRef, in your code it's just ref, so the following should work, untested though:



    // get notified when the download URL is available
task.snapshotChanges().pipe(
finalize(() => this.downloadURL = ref.getDownloadURL() )
)
.subscribe()





share|improve this answer
























  • Thanks Jeremy. Just one last thing. I get the following error on testing: [ts] Cannot find name 'finalize'. [2304]. Am I missing an import or something?

    – altergothen
    Nov 26 '18 at 20:18








  • 1





    Sounds like it, you'll need import { finalize } from 'rxjs/operators'; at the top of that component

    – JeremyW
    Nov 26 '18 at 20:24













  • Hi @JeremyW. Please can you kindly assist with follow up question? Thanks ;-) stackoverflow.com/questions/53853189/…

    – altergothen
    Jan 9 at 0:44












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53480280%2fangular-fire-angularfirestorage-getdownloadurl%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














There are some other minor structure changes in the code too, not just renaming the method.



Check out the example code in the official AngularFire2 docs under the section titled "Monitoring upload percentage". Specifically, the example upload function they include:



uploadFile(event) {
const file = event.target.files[0];
const filePath = 'name-your-file-path-here';
const fileRef = this.storage.ref(filePath);
const task = this.storage.upload(filePath, file);

// observe percentage changes
this.uploadPercent = task.percentageChanges();

// get notified when the download URL is available
task.snapshotChanges().pipe(
finalize(() => this.downloadURL = fileRef.getDownloadURL() )
)
.subscribe()
}


and even more specifically, this part of that function, the listener....



// get notified when the download URL is available
task.snapshotChanges().pipe(
finalize(() => this.imageUrl = fileRef.getDownloadURL() )
)
.subscribe()


It will fire when the upload has finalized and populate the this.downloadURL variable with the URL. You've already defined the fileRef, in your code it's just ref, so the following should work, untested though:



    // get notified when the download URL is available
task.snapshotChanges().pipe(
finalize(() => this.downloadURL = ref.getDownloadURL() )
)
.subscribe()





share|improve this answer
























  • Thanks Jeremy. Just one last thing. I get the following error on testing: [ts] Cannot find name 'finalize'. [2304]. Am I missing an import or something?

    – altergothen
    Nov 26 '18 at 20:18








  • 1





    Sounds like it, you'll need import { finalize } from 'rxjs/operators'; at the top of that component

    – JeremyW
    Nov 26 '18 at 20:24













  • Hi @JeremyW. Please can you kindly assist with follow up question? Thanks ;-) stackoverflow.com/questions/53853189/…

    – altergothen
    Jan 9 at 0:44
















3














There are some other minor structure changes in the code too, not just renaming the method.



Check out the example code in the official AngularFire2 docs under the section titled "Monitoring upload percentage". Specifically, the example upload function they include:



uploadFile(event) {
const file = event.target.files[0];
const filePath = 'name-your-file-path-here';
const fileRef = this.storage.ref(filePath);
const task = this.storage.upload(filePath, file);

// observe percentage changes
this.uploadPercent = task.percentageChanges();

// get notified when the download URL is available
task.snapshotChanges().pipe(
finalize(() => this.downloadURL = fileRef.getDownloadURL() )
)
.subscribe()
}


and even more specifically, this part of that function, the listener....



// get notified when the download URL is available
task.snapshotChanges().pipe(
finalize(() => this.imageUrl = fileRef.getDownloadURL() )
)
.subscribe()


It will fire when the upload has finalized and populate the this.downloadURL variable with the URL. You've already defined the fileRef, in your code it's just ref, so the following should work, untested though:



    // get notified when the download URL is available
task.snapshotChanges().pipe(
finalize(() => this.downloadURL = ref.getDownloadURL() )
)
.subscribe()





share|improve this answer
























  • Thanks Jeremy. Just one last thing. I get the following error on testing: [ts] Cannot find name 'finalize'. [2304]. Am I missing an import or something?

    – altergothen
    Nov 26 '18 at 20:18








  • 1





    Sounds like it, you'll need import { finalize } from 'rxjs/operators'; at the top of that component

    – JeremyW
    Nov 26 '18 at 20:24













  • Hi @JeremyW. Please can you kindly assist with follow up question? Thanks ;-) stackoverflow.com/questions/53853189/…

    – altergothen
    Jan 9 at 0:44














3












3








3







There are some other minor structure changes in the code too, not just renaming the method.



Check out the example code in the official AngularFire2 docs under the section titled "Monitoring upload percentage". Specifically, the example upload function they include:



uploadFile(event) {
const file = event.target.files[0];
const filePath = 'name-your-file-path-here';
const fileRef = this.storage.ref(filePath);
const task = this.storage.upload(filePath, file);

// observe percentage changes
this.uploadPercent = task.percentageChanges();

// get notified when the download URL is available
task.snapshotChanges().pipe(
finalize(() => this.downloadURL = fileRef.getDownloadURL() )
)
.subscribe()
}


and even more specifically, this part of that function, the listener....



// get notified when the download URL is available
task.snapshotChanges().pipe(
finalize(() => this.imageUrl = fileRef.getDownloadURL() )
)
.subscribe()


It will fire when the upload has finalized and populate the this.downloadURL variable with the URL. You've already defined the fileRef, in your code it's just ref, so the following should work, untested though:



    // get notified when the download URL is available
task.snapshotChanges().pipe(
finalize(() => this.downloadURL = ref.getDownloadURL() )
)
.subscribe()





share|improve this answer













There are some other minor structure changes in the code too, not just renaming the method.



Check out the example code in the official AngularFire2 docs under the section titled "Monitoring upload percentage". Specifically, the example upload function they include:



uploadFile(event) {
const file = event.target.files[0];
const filePath = 'name-your-file-path-here';
const fileRef = this.storage.ref(filePath);
const task = this.storage.upload(filePath, file);

// observe percentage changes
this.uploadPercent = task.percentageChanges();

// get notified when the download URL is available
task.snapshotChanges().pipe(
finalize(() => this.downloadURL = fileRef.getDownloadURL() )
)
.subscribe()
}


and even more specifically, this part of that function, the listener....



// get notified when the download URL is available
task.snapshotChanges().pipe(
finalize(() => this.imageUrl = fileRef.getDownloadURL() )
)
.subscribe()


It will fire when the upload has finalized and populate the this.downloadURL variable with the URL. You've already defined the fileRef, in your code it's just ref, so the following should work, untested though:



    // get notified when the download URL is available
task.snapshotChanges().pipe(
finalize(() => this.downloadURL = ref.getDownloadURL() )
)
.subscribe()






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 26 '18 at 15:12









JeremyWJeremyW

2,173717




2,173717













  • Thanks Jeremy. Just one last thing. I get the following error on testing: [ts] Cannot find name 'finalize'. [2304]. Am I missing an import or something?

    – altergothen
    Nov 26 '18 at 20:18








  • 1





    Sounds like it, you'll need import { finalize } from 'rxjs/operators'; at the top of that component

    – JeremyW
    Nov 26 '18 at 20:24













  • Hi @JeremyW. Please can you kindly assist with follow up question? Thanks ;-) stackoverflow.com/questions/53853189/…

    – altergothen
    Jan 9 at 0:44



















  • Thanks Jeremy. Just one last thing. I get the following error on testing: [ts] Cannot find name 'finalize'. [2304]. Am I missing an import or something?

    – altergothen
    Nov 26 '18 at 20:18








  • 1





    Sounds like it, you'll need import { finalize } from 'rxjs/operators'; at the top of that component

    – JeremyW
    Nov 26 '18 at 20:24













  • Hi @JeremyW. Please can you kindly assist with follow up question? Thanks ;-) stackoverflow.com/questions/53853189/…

    – altergothen
    Jan 9 at 0:44

















Thanks Jeremy. Just one last thing. I get the following error on testing: [ts] Cannot find name 'finalize'. [2304]. Am I missing an import or something?

– altergothen
Nov 26 '18 at 20:18







Thanks Jeremy. Just one last thing. I get the following error on testing: [ts] Cannot find name 'finalize'. [2304]. Am I missing an import or something?

– altergothen
Nov 26 '18 at 20:18






1




1





Sounds like it, you'll need import { finalize } from 'rxjs/operators'; at the top of that component

– JeremyW
Nov 26 '18 at 20:24







Sounds like it, you'll need import { finalize } from 'rxjs/operators'; at the top of that component

– JeremyW
Nov 26 '18 at 20:24















Hi @JeremyW. Please can you kindly assist with follow up question? Thanks ;-) stackoverflow.com/questions/53853189/…

– altergothen
Jan 9 at 0:44





Hi @JeremyW. Please can you kindly assist with follow up question? Thanks ;-) stackoverflow.com/questions/53853189/…

– altergothen
Jan 9 at 0:44




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53480280%2fangular-fire-angularfirestorage-getdownloadurl%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Costa Masnaga

Fotorealismo

Create new schema in PostgreSQL using DBeaver