I don't know why but when I send file to server the current page is reloaded in the same time... That happens also with Postman.
I use a copy of the official doc.
And I use SlimFramework :
Route :
$app->post('/api/file/upload', FileController::class . ':add');
Controller :
public function add (Request $request, Response $response, $args) {
$imagepath = '';
$files = $request->getUploadedFiles();
$newfile = $files['file'];
if($newfile->getError() === UPLOAD_ERR_OK) {
$uploadFilename = $newfile->getClientFilename();
$newfile->moveTo('../public/src/assets/img/' . $uploadFilename);
$imagepath = "assets/img/" . $uploadFilename;
}
$addIconQuery = 'UPDATE categories SET icon = :icon WHERE id_category = :id';
$stmt = $this->container->get('db')->prepare($addIconQuery);
$stmt->execute([
':id' => 21,
':icon' => $imagepath
]);
}
The file is correctly uploaded and the path is added in database.
Any idea ?
UPDATE
<div class="field">
<input type="file" name="file" ng2FileSelect [uploader]="uploader">
<button type="button" class="btn btn-success btn-xs" (click)="uploader.uploadAll()">
<span class="glyphicon glyphicon-upload"></span> Upload
</button>
</div>
In Component
public uploader: FileUploader = new FileUploader({ url: Config.apiUrl + 'api/file/upload' });
ngOnInit() {
this.uploader.onAfterAddingFile = (file) => { file.withCredentials = false; };
this.uploader.onCancelItem = (item:any, response: any, status: any, header:any) => {
console.log('ImageUpload:uploaded:', item, status, response);
}
}