如何在 Ionic 4 中将图片上传到 firebase?
How to upload pictures to firebase in Ionic 4?
我无法将图片上传到 Firebase。任何帮助将不胜感激。
有没有人熟悉如何在Ionic 4.0中上传图片到firebase?下面是用于在 Ionic 2 中工作的代码,但是现在当我单击按钮将图像上传到 firebase 时,它需要大约 30 秒来响应(单击),然后它永远不会将图像上传到 firebase。
我尝试使用本教程中的示例,但无法摆脱与 ImagePicker 相关的错误。 https://ionicthemes.com/tutorials/about/ionic-firebase-image-upload
任何帮助将不胜感激。
<ion-card-content>
<div>
<img src="assets/img/add-an-image.png" (click)="selectPhoto()"/>
</div>
</ion-card-content>
constructor(
private afAuth: AngularFireAuth,
private camera: Camera) {
this.afAuth.authState.subscribe(user => {
this.userId = user.uid;
this.myPhotosRef = firebase.storage().ref(`/Photos/${ this.userId }/`);
});
}
selectPhoto(): void {
const options: CameraOptions = {
quality: 100,
destinationType: this.camera.DestinationType.FILE_URI,
encodingType: this.camera.EncodingType.JPEG,
mediaType: this.camera.MediaType.PICTURE,
sourceType: this.camera.PictureSourceType.PHOTOLIBRARY,
};
this.camera.getPicture(options).then((imageData) => {
console.log(options, 'get pic');
this.myPhoto = imageData;
this.uploadPhoto(this.myPhoto);
}, error => {
console.log('ERROR -> ' + JSON.stringify(error));
});
}
private uploadPhoto(photoName: string): void {
this.myPhotosRef.child(photoName)
.putString(this.myPhoto, 'base64', { contentType: 'image/png' })
.then((savedPicture) => {
this.myPhotoURL = savedPicture.downloadURL;
});
}
确实有很多方法可以做到这一点,您可以使用响应和错误来跟踪发生了什么。启用 Web 控制台日志。
以下是如何执行此操作并跟踪错误的示例:
uploadFile(file){
const fileName = this.generateRandomName() + '.jpg';
const fileRef = firebase.storage().ref().child('tptimagefolder/'+ fileName);
const uploadTask = fileRef.put(file);
return new Promise((resolve, reject) => {
uploadTask.on('state_changed', snapshot => {
}, error => {
reject(error);
}, () => {
uploadTask.snapshot.ref.getDownloadURL().then(function(downloadURL) {
console.log('teste resolve url'+downloadURL);
resolve ( {fileName, downloadURL});
});
});
}); }
this.generateRandomName()是一个创建名字的函数,不用它也可以不用。
我能够使用以下教程成功将图像上传到 firebase:
https://ionicthemes.com/tutorials/about/building-a-ionic-firebase-app-step-by-step
我按照 Ionic Themes 中的教程将图像上传到 Firebase 存储:https://ionicthemes.com/tutorials/about/ionic-firebase-image-upload
它说它适用于 Ionic 3,但它也适用于 Ionic 4。
根据教程,Storage 与 firestore 和 fire storage 模块是分开的,所以在你的应用程序模块中,你必须像这样导入 firebase:
import * as firebase from "firebase";
firebase.initializeApp(environment.firebaseConfig)
在 Ionic 4 应用程序的 Firestore 上上传图片
查看本教程link
我无法将图片上传到 Firebase。任何帮助将不胜感激。
有没有人熟悉如何在Ionic 4.0中上传图片到firebase?下面是用于在 Ionic 2 中工作的代码,但是现在当我单击按钮将图像上传到 firebase 时,它需要大约 30 秒来响应(单击),然后它永远不会将图像上传到 firebase。
我尝试使用本教程中的示例,但无法摆脱与 ImagePicker 相关的错误。 https://ionicthemes.com/tutorials/about/ionic-firebase-image-upload 任何帮助将不胜感激。
<ion-card-content>
<div>
<img src="assets/img/add-an-image.png" (click)="selectPhoto()"/>
</div>
</ion-card-content>
constructor(
private afAuth: AngularFireAuth,
private camera: Camera) {
this.afAuth.authState.subscribe(user => {
this.userId = user.uid;
this.myPhotosRef = firebase.storage().ref(`/Photos/${ this.userId }/`);
});
}
selectPhoto(): void {
const options: CameraOptions = {
quality: 100,
destinationType: this.camera.DestinationType.FILE_URI,
encodingType: this.camera.EncodingType.JPEG,
mediaType: this.camera.MediaType.PICTURE,
sourceType: this.camera.PictureSourceType.PHOTOLIBRARY,
};
this.camera.getPicture(options).then((imageData) => {
console.log(options, 'get pic');
this.myPhoto = imageData;
this.uploadPhoto(this.myPhoto);
}, error => {
console.log('ERROR -> ' + JSON.stringify(error));
});
}
private uploadPhoto(photoName: string): void {
this.myPhotosRef.child(photoName)
.putString(this.myPhoto, 'base64', { contentType: 'image/png' })
.then((savedPicture) => {
this.myPhotoURL = savedPicture.downloadURL;
});
}
确实有很多方法可以做到这一点,您可以使用响应和错误来跟踪发生了什么。启用 Web 控制台日志。
以下是如何执行此操作并跟踪错误的示例:
uploadFile(file){
const fileName = this.generateRandomName() + '.jpg';
const fileRef = firebase.storage().ref().child('tptimagefolder/'+ fileName);
const uploadTask = fileRef.put(file);
return new Promise((resolve, reject) => {
uploadTask.on('state_changed', snapshot => {
}, error => {
reject(error);
}, () => {
uploadTask.snapshot.ref.getDownloadURL().then(function(downloadURL) {
console.log('teste resolve url'+downloadURL);
resolve ( {fileName, downloadURL});
});
});
}); }
this.generateRandomName()是一个创建名字的函数,不用它也可以不用。
我能够使用以下教程成功将图像上传到 firebase:
https://ionicthemes.com/tutorials/about/building-a-ionic-firebase-app-step-by-step
我按照 Ionic Themes 中的教程将图像上传到 Firebase 存储:https://ionicthemes.com/tutorials/about/ionic-firebase-image-upload
它说它适用于 Ionic 3,但它也适用于 Ionic 4。
根据教程,Storage 与 firestore 和 fire storage 模块是分开的,所以在你的应用程序模块中,你必须像这样导入 firebase:
import * as firebase from "firebase";
firebase.initializeApp(environment.firebaseConfig)
在 Ionic 4 应用程序的 Firestore 上上传图片
查看本教程link