Я пытаюсь загрузить изображение в свою базу данных в Ionic 4. При использовании симулятора я могу отобразить изображение из библиотеки, однако при попытке загрузить его не удается. Однако API работает в Почтальоне.
Также при развертывании на устройстве изображения не отображаются из-за ошибки веб-просмотра, говорящей о том, что плагин не установлен.
upload.ts
readFile(file: any) {
const reader = new FileReader();
reader.onloadend = () => {
const formData = new FormData();
const imgBlob = new Blob([reader.result], {
type: file.type
});
formData.append('file', imgBlob, file.name);
this.uploadImageData(formData);
};
reader.readAsArrayBuffer(file);
}
async uploadImageData(formData: FormData) {
const loading = await this.loadingController.create({
message: 'Uploading image...',
});
console.log('Uploading');
await loading.present();
this.http.post('http://ccoulter12.lampt.eeecs.qub.ac.uk/api/image.php', formData)
.pipe(
finalize(() => {
loading.dismiss();
})
)
.subscribe(res => {
console.log(res);
// Think this is the problem
if (res['success']) {
this.presentToast('File upload complete.');
} else {
this.presentToast('File upload failed.');
}
});
}
бэкенд
$target_path = "uploads/";
$target_path = $target_path . basename( $_FILES['file']['name']);
if (move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) {
header('Content-type: application/json');
$data = ['success' => true, 'message' => 'Upload and move success'];
echo json_encode( $data );
} else{
header('Content-type: application/json');
$data = ['success' => false, 'message' => 'There was an error uploading the file, please try again!'];
echo json_encode( $data );
}