Hey there! I've got a project on my localhost where I need to upload files to a local folder. I'm sharing the code here in hopes that someone can assist me.
HTML:
<ion-item ion-item *ngFor="let item of lista" menuClose>
Floor: {{item.piso}} - Number: {{item.numero}}
<input type="file" (change)="handleFileInput($event.target.files)" id="file-input"
accept="image/png, image/jpeg, application/pdf">
<ion-icon item-end (click)="cargarExpensas(item)" title="Upload" style="cursor:pointer" name="ios-cloud-upload-outline">
</ion-icon>
</ion-item>
Typescript:
handleFileInput(files: FileList) {
this.fileToUpload = files.item(0);}
cargarExpensas() {
this.servicioUpload.postFile(this.fileToUpload).subscribe(data => {
let toast = this.toastCtrl.create({
message: 'Expense uploaded successfully',
duration: 3500,
cssClass: "clsToastCtrl",
});
toast.present();
}, error => {
let toast = this.toastCtrl.create({
message: 'An error occurred, please try again',
duration: 3500,
cssClass: "clsToastCtrlError",
});
toast.present();
console.log(error);
}); }
Provider:
postFile(fileToUpload: File): Observable<boolean> {
const endpoint = apiUrl + 'api/upload/files/';
const formData: FormData = new FormData();
formData.append('fileKey', fileToUpload, fileToUpload.name);
return this.http
.post(endpoint, formData, { headers:new HttpHeaders({
'Content-Type': 'application/json'})
})
.map(() => { return true; })
}
I need help with the PHP code for this using the Slim framework. Can anyone lend a hand? Also, any feedback on the code above would be greatly appreciated. Thanks a bunch!