im using Ionic 2 and i want to upload image from my device to using codeigniter as my api im stuck searching for this but no luck
thankyou for your help.
1 Like
hi, you must send your image with Base64Encoded , just add this code in your html page :
<form [formGroup]="form" (ngSubmit)="onSubmit()">
<div class="form-group">
<input type="file" id="avatar" (change)="onFileChange($event)" #fileInput>
</div>
in your .td add :
onFileChange(event) {
let reader = new FileReader();
if(event.target.files && event.target.files.length > 0) {
let file = event.target.files[0];
reader.readAsDataURL(file);
reader.onload = () => {
this.form.get('avatar').setValue({
filename: file.name,
filetype: file.type,
value: reader.result.split(',')[1]
})
};
}
}
onSubmit() {
const formModel = this.form.value;
var body = {'name':formModel.avatar.filename,'content':formModel.avatar.value};
let headers = new HttpHeaders({ 'Content-Type': 'application/json' });
headers.append('Content-Type', 'application/json; charset=UTF-8');
this.http.post(this.Api.Url,body,{ headers: headers}).subscribe(data => {
return data;
});
}
your api will receive an object with name and content , so you need to convert content from Base64Encoded to image .
Hi thanks for the answer actually i have already get the image but on codeigniter when i pass tha image to upload to server not saving it returns no selected file
Post the data that you are sending. I suggest convert your image to base64 then send it to codeigniter API and check if the codeigniter received your data.