Hi,
I’m trying to upload files from my ionic 3 app, but since the file transfer plugin is deprecated, is there a way to upload files with angular’s Http @angular/http?
Here is my current code:
//this variable stores the image path that I need to upload
lastImage: string = null;
//...
//a button in my html calls this function
public takePicture(sourceType) {
// Create options for the Camera Dialog
let options = {
quality: 100,
sourceType: sourceType,
saveToPhotoAlbum: false,
correctOrientation: true
};
// Get the data of an image
this.camera.getPicture(options).then((imagePath) => {
// Special handling for Android library
if (this.platform.is('android') && sourceType === this.camera.PictureSourceType.PHOTOLIBRARY) {
this.filePath.resolveNativePath(imagePath)
.then(filePath => {
let correctPath = filePath.substr(0, filePath.lastIndexOf('/') + 1);
let currentName = imagePath.substring(imagePath.lastIndexOf('/') + 1, imagePath.lastIndexOf('?'));
this.copyFileToLocalDir(correctPath, currentName, this.createFileName());
});
} else {
let currentName = imagePath.substr(imagePath.lastIndexOf('/') + 1);
let correctPath = imagePath.substr(0, imagePath.lastIndexOf('/') + 1);
this.copyFileToLocalDir(correctPath, currentName, this.createFileName());
}
}, (err) => {
this.presentToast('Error');
});
}
private createFileName() {
return new Date().getTime() + ".jpg";
}
private copyFileToLocalDir(namePath, currentName, newFileName) {
this.file.copyFile(namePath, currentName, cordova.file.dataDirectory, newFileName).then(success => {
this.lastImage = newFileName;
}, error => {
this.presentToast('Error saving the image');
});
}
When I take a picture I have the lastImage variable with the path of the image that I need to upload.
So, how can I send the image with an @angular/http post request?
do you have a working example with the @ionic-native/camera plugin and uploading without the transfer plugin? I’m struggling with this because I only have the file path to the picture like in your example and I cannot get this working with angular to send it to my s3 endpoint.
With the deprecated plugin I’m using the following:
var formData = new FormData();
var blob = new Blob(['Lorem ipsum'], { type: 'plain/text' });
formData.append('file', blob,'readme.txt');
var request = new XMLHttpRequest();
request.open('POST', 'http://example.org/upload');
request.send(formData);
i tried ur code but its not working for me i got “No Image Selected”
main.js:487 “Unable to retrieve path to picture!” error ,can you tell me how to fix?