Skip to content

Instantly share code, notes, and snippets.

@Tusharbalar
Last active October 12, 2020 01:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Tusharbalar/ce96882920de299e4e396d91c3388c0f to your computer and use it in GitHub Desktop.
Save Tusharbalar/ce96882920de299e4e396d91c3388c0f to your computer and use it in GitHub Desktop.
Ionic3 - Image upload using Camera, Gallery and File
/*
* component file code
*/
/*
* IMAGE UPLOAD SECTION
*/
public takePicture(sourceType) {
let options = {
quality: 30,
allowEdit: true,
sourceType: sourceType,
saveToPhotoAlbum: false,
correctOrientation: true,
encodingType: this.camera.EncodingType.JPEG,
mediaType: this.camera.MediaType.PICTURE
}
this.camera.getPicture(options).then((imagePath) => {
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 {
var currentName = imagePath.substr(imagePath.lastIndexOf('/') + 1);
var correctPath = imagePath.substr(0, imagePath.lastIndexOf('/') + 1);
this.copyFileToLocalDir(correctPath, currentName, this.createFileName());
}
});
}
lastImage: string = null;
// Create a new name for the image
private createFileName() {
var d = new Date(),
n = d.getTime(),
newFileName = n + ".jpg";
return newFileName;
}
private copyFileToLocalDir(namePath, currentName, newFileName) {
this.file.copyFile(namePath, currentName, this.file.dataDirectory, newFileName).then(success => {
this.lastImage = newFileName;
let filePath = this.pathForImage(this.lastImage);
// Write code for save data to database
}, error => {
// alert(error);
this.notPost = true;
});
}
// Always get the accurate path to your apps folder
public pathForImage(img) {
if (img === null) {
return '';
} else {
return this.file.dataDirectory + img;
}
}
/*
* Service code (used file transfer plugin)
*/
public uploadImage(messageId, fileName, filePath) {
// Destination URL
let url = this.serverUrl + "/" + messageId + "/picture";
// File for Upload
var targetPath = filePath;
// File name only
var filename = fileName;
var options = {
fileKey: "file",
fileName: filename,
chunkedMode: false,
mimeType: "multipart/form-data",
params : {'fileName': filename},
headers: {
'Content-Type': undefined,
'Authorization': 'Bearer ' + localStorage.getItem('access_token')
}
};
const fileTransfer: FileTransferObject = this.transfer.create();
// Use the FileTransfer to upload the image
return fileTransfer.upload(targetPath, url, options).then(data => {
return data;
}, (err) => {
return err;
// alert("Errrr"+ JSON.stringify(err));
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment