我试图将Blob文件上传到Firebase,但我一直收到“undefined”作为“makeFileIntoBlob”函数的返回。我在Android上运行它。 我可以在拍照后获取文件路径,但是当我调用该函数来检索Blob并将其上传到Firebase时,我将Blob文件设置为“未定义”。 一些帮助?将文件转换为TypeScript上的Blob for Ionic
继承人,我把图片上传到火力地堡存储
uploadToFirebase(imageBlob) {
var fileName = 'sample.jpg';
return new Promise((resolve, reject) => {
var fileRef = firebase.storage().ref('Animais/' + fileName);
var uploadTask = fileRef.put(imageBlob);
uploadTask.on('state_changed', (snapshot) => {
console.log('snapshot progess ' + snapshot);
}, (_error) => {
reject(_error);
},() => {
// completion...
resolve(uploadTask.snapshot);
});
});
}
继承人的功能,我尝试从文件路径
makeFileIntoBlob(imagePath) {
var reader = new FileReader();
reader.onloadend = (evt: any) => {
var imgBlob: any = new Blob(imagePath);
imgBlob.name = 'sample.jpg';
return imgBlob;
};
reader.onerror = (e) => {
console.log('Failed file read: ' + e.toString());
};
继承人在那里我拍照
的功能做一个斑点的功能takePic(){
let imageSource;
this.camera.getPicture({
destinationType: this.camera.DestinationType.FILE_URI,
sourceType: imageSource,
encodingType: this.camera.EncodingType.JPEG,
targetHeight: 640,
correctOrientation: true,
saveToPhotoAlbum : true
}).then((imagePath) => {
alert('got image path ' + imagePath);
// convert picture to blob
return this.makeFileIntoBlob(imagePath);
}).then((imageBlob) => {
alert('got image blob ' + imageBlob);
// upload the blob
return this.uploadToFirebase(imageBlob);
}).then((uploadSnapshot: any) => {
alert('file uploaded successfully ' + uploadSnapshot.downloadURL);
// store reference to storage in database
return this.saveToDatabaseAssetList(uploadSnapshot);
}).then((uploadSnapshot: any) => {
//alert('file saved to asset catalog successfully ');
}, (error) => {
alert('Error ' + (error.message || error));
});
你真的在使用TypeScript吗?你得到什么编译器错误?什么是BLOB? – jcalz
是的,Typescript,Ionic3。抱歉,BLOB,只是我正在尝试的调试,已经编辑它。我没有收到编译器错误,我只是将“undefined”作为函数“makeFileIntoBlob”的返回值。 –
这可能是[的Javascript少辉用的FileReader()](https://stackoverflow.com/questions/34495796/javascript-promises-with-filereader),因为你的问题是,你使用的是'FileReader'但重复不使用其回调来解决/拒绝“承诺”。 – jcalz