【发布时间】:2019-01-09 01:44:12
【问题描述】:
我可以通过这种方式轻松上传图片(选择输入文件):
import { AngularFireStorage } from 'angularfire2/storage';
@Component({
selector: 'app-root',
template: '<div>'
+ '<input class="file-input" type="file" (change)="selFile($event.target.files)">'
+ '<button (click)="startUpload()">Upload</button>'
+ '</div>',
})
export class AppComponent {
constructor(
private storage: AngularFireStorage
) {}
selectedFile: any;
selFile(event: FileList) {
this.selectedFile = event.item(0);
}
startUpload() {
const file = this.selectedFile;
const path = `test/${new Date().getTime()}_${file.name}`;
this.task = this.storage.upload(path, file)
}
}
这样,一切正常。
但我想上传一张使用@ionic-native/camera 拍摄的图片,如下所示:
import { AngularFireStorage } from 'angularfire2/storage';
import { Camera } from '@ionic-native/camera';
@Component({
template: '<div>'
+ '<img src={{pathForImage(lastImage)}} style="width: 100%">'
+ '<button (click)="takePic()">Take Picture</button>'
+ '<button (click)="startUpload()">Upload</button>'
+ '</div>',
})
export class AppComponent {
constructor(
private storage: AngularFireStorage,
private camera: Camera,
) {}
selectedFile: any;
selFile(event: FileList) {
this.selectedFile = event.item(0);
}
startUpload() {
const file = this.selectedFile;
const path = `test/${new Date().getTime()}_${file.name}`;
this.task = this.storage.upload(path, file)
}
public takePic() {
var options = {
quality: 100,
sourceType: sourceType,
saveToPhotoAlbum: false,
correctOrientation: true
};
this.camera.getPicture(options).then((imagePath) => {
var currentName = imagePath.substr(imagePath.lastIndexOf('/') + 1);
var correctPath = imagePath.substr(0, imagePath.lastIndexOf('/') + 1);
this.copyFileToLocalDir(correctPath, currentName, this.createFileName());
}
}
private copyFileToLocalDir(namePath, currentName, newFileName) {
this.file.copyFile(namePath, currentName, cordova.file.dataDirectory, newFileName).then(success => {
this.lastImage = newFileName;
});
}
public pathForImage(img) {
if (img === null) {
return '';
} else {
return cordova.file.dataDirectory + img;
}
}
}
所以,我可以将图像上传到 Firebase 存储。我可以用@ionic-native/camera 拍照。
但我不知道如何将使用相机拍摄的图像上传到 Firebase 存储。
如何像第一个示例一样上传 {{pathForImage(lastImage)}} 中的图像???
【问题讨论】:
-
您想从输入中选择图像。接下来,在img标签中显示并点击按钮上传?
-
我有选择文件或拍照的代码。这工作正常。结果存储在 myImageToUpload 中。但我不知道如何上传 myImageToUpload。
标签: javascript angular firebase ionic3 firebase-storage