I would like to upload an image to my back-end using FormData, but as Ionic DEVAPP and Ionic VIEW does not support file, file-transfer and file-upload plugins, I need to do it using only Angular Http or HttpClient.
When using DestinationType.FILE_URI, i can get the internal url from the file and display it on a img object, but I can't create a typescript File object from this url without the native file, file-path and file-transfer plugins.
getImage() {
const options: CameraOptions = {
quality: 100,
destinationType: this.camera.DestinationType.FILE_URI,
sourceType: this.camera.PictureSourceType.PHOTOLIBRARY
}
this.camera.getPicture(options).then((imageData) => {
this.imageURI = this.sanitizer.bypassSecurityTrustUrl(imageData)
}, (err) => {
console.log(err)
this.presentToast(err)
})
}
using this template
<ion-content padding>
<ion-item>
<p>{{imageFileName}}</p>
<button ion-button color="secondary" (click)="getImage()">Get Image</button>
</ion-item>
<ion-item>
<h4>Image Preview</h4>
<img style="display:block" [src]="imageURI" *ngIf="imageURI" alt="Ionic File" width="300" />
</ion-item>
<ion-item>
<button ion-button (click)="uploadFile()">Upload</button>
</ion-item>
</ion-content>
When using DestinationType.DATA_URL I can display the image but can not create the typescript File object needed with the original file name to append the image to the FormData used on my upload service at my Ionic App.
It seems I can find a way to create this typescript File object with the original filename from FILE_URI and a the base64 encoded data from DATA_URL using the camera.getPicture from the cordova camera native plugin.
Service to upload the file to my back-end just uses this approach:
postImage(image: File): Observable<any> {
const formData = new FormData()
.append('file', image, image.name)
}
return this.httpClient.post<any>(this.myUrl,formData)
}
Both getImage from component myPage.ts and postImage from uploadservice.ts work fine, but I can't find a way to create the File object from camera.getPicture imageData