angular - ionic 2 firebase uploading image -
i creating chat application ionic 3 , firebase. in profile pic section allowing user upload image. image uploading , visible in firebase storage, every image uploaded user of 4 bytes , preview not available hence, profile image not set new image. kindly on how fix kind regards, aditya
imagehandler.ts(that uploads file firebase) import { injectable } '@angular/core'; import { file } '@ionic-native/file'; import { filechooser } '@ionic-native/file-chooser'; import { filepath } '@ionic-native/file-path'; import firebase 'firebase'; /* generated class imghandlerprovider provider. see https://angular.io/docs/ts/latest/guide/dependency-injection.html more info on providers , angular 2 di. */ @injectable() export class imghandlerprovider { nativepath: any; firestore = firebase.storage(); constructor(public filechooser: filechooser) { } uploadimage() { var promise = new promise((resolve, reject) => { this.filechooser.open().then((url) => { (<any>window).filepath.resolvenativepath(url, (result) => { this.nativepath = result; (<any>window).resolvelocalfilesystemurl(this.nativepath, (res) => { res.file((resfile) => { var reader = new filereader(); reader.readasarraybuffer(resfile); reader.onloadend = (evt: any) => { var imgblob = new blob([evt.target.result], { type: 'image/jpeg' }); var imagestore = this.firestore.ref('/profileimages').child(firebase.auth().currentuser.uid); imagestore.put(imgblob).then((res) => { this.firestore.ref('/profileimages').child(firebase.auth().currentuser.uid).getdownloadurl().then((url) => { resolve(url); }).catch((err) => { reject(err); }) }).catch((err) => { reject(err); }) } }) }) }) }) }) return promise; } }
user.ts(that handles user methods)
import { injectable } '@angular/core'; import { angularfireauth } 'angularfire2/auth'; import firebase 'firebase'; /* generated class userprovider provider. see https://angular.io/docs/ts/latest/guide/dependency-injection.html more info on providers , angular 2 di. */ @injectable() export class userprovider { firedata = firebase.database().ref('/users'); constructor(public afireauth: angularfireauth) { } adduser(newuser) { var promise = new promise((resolve, reject) => { this.afireauth.auth.createuserwithemailandpassword(newuser.email, newuser.password).then(() => { this.afireauth.auth.currentuser.updateprofile({ displayname: newuser.displayname, photourl: '' }).then(() => { this.firedata.child(this.afireauth.auth.currentuser.uid).set({ uid: this.afireauth.auth.currentuser.uid, displayname: newuser.displayname, photourl: 'https://firebasestorage.googleapis.com/v0/b/myapp-4eadd.appspot.com/o/chatterplace.png?alt=media&token=e51fa887-bfc6-48ff-87c6-e2c61976534e' }).then(() => { resolve({ success: true }); }).catch((err) => { reject(err); }) }).catch((err) => { reject(err); }) }).catch((err) => { reject(err); }) }) return promise; } passwordreset(email) { var promise = new promise((resolve, reject) => { firebase.auth().sendpasswordresetemail(email).then(() => { resolve({ success: true }); }).catch((err) => { reject(err); }) }) return promise; } updateimage(imageurl) { var promise = new promise((resolve, reject) => { this.afireauth.auth.currentuser.updateprofile({ displayname: this.afireauth.auth.currentuser.displayname, photourl: imageurl }).then(() => { firebase.database().ref('/users/' + firebase.auth().currentuser.uid).update({ displayname: this.afireauth.auth.currentuser.displayname, photourl: imageurl, uid: firebase.auth().currentuser.uid }).then(() => { resolve({ success: true }); }).catch((err) => { reject(err); }) }).catch((err) => { reject(err); }) }) return promise; } }
Comments
Post a Comment