John Davidson

Can't upload file "Undefined index: imageData" with Angular and PHP

0 comments
Message:


I need to upload file with PrimeNG and found data in FormData when console.log but PHP response Undefined index: imageData


My HTML Code :


<p-fileUpload name="imageData" id="imageData" customUpload="true" mode="basic" (onSelect)="onFileSelected($event)" (uploadHandler)="onUpload()"></p-fileUpload>

TS Code :


selectFile: File;


onFileSelected(event) {
this.selectFile = event.files[0];
}


onUpload() {
const myheaders = { 'Content-type': 'undefined' };
const formData = new FormData();
formData.append('imageData', this.selectFile, this.selectFile.name);
console.log(formData.getAll('imageData'));
this.http
.post<any>('http://localhost/project/api/upload.php', formData, {
reportProgress: true,
observe: 'events',
headers: myheaders
})
.subscribe(
(res) => {}
);
}

when console.log(formData.getAll('imageData')); i get :



when upload I get Undefined index: imageData


PHP Code :


<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");
header('Access-Control-Allow-Headers: X-Requested-With, content-type, access-control-allow-origin, access-control-allow-methods, access-control-allow-headers');

include('./db_connect.php');

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

$content = file_get_contents('php://input');
$myfile = json_decode($content, true);

$imageData = json_encode($_FILES['imageData']);

echo $imageData;

}

mysqli_close($conn);

and I try to send data with POSTMAN PHP response is :




My Answer:
I help you with something specific?

Rate this post

5 of 5 based on 3044 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us