I am using React.js as a frontend and I'm trying to send a .csv
file to my backend which is written in PHP.
PHP doesn't seem to be accepting my csv files. I have tested with other file type (PDF, PNG, JPG) and all of these turning up on the backend.
I have also tried to change Content-Type to multipart/form-data
and text/csv
on both axios and php backend.
<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: GET, POST");
header("Access-Control-Allow-Headers: Content-Disposition, Content-Type, Content-Length, Accept-Encoding");
header("Content-type: multipart/form-data");
require_once 'jwt_utils.php';
$json_params = file_get_contents("php://input");
if (strlen($json_params) > 0) {
$decoded_params = json_decode($json_params);
}
$output = "No request recognised";
if (json_last_error() != JSON_ERROR_NONE) {
echo ($output);
exit(0);
}
// if($_FILES){
// $file = $_FILES["data"];
// }
echo json_encode(["status"=>$status, "json"=>$decoded_params, "_File"=>$_FILES]);
?>
The above PHP doesn't even return a a json encoded string. It returns HTML like:
<!DOCTYPE html>\r\n\r\n<html lang=\"en\">\r\n \ .....
uploadEcg.js
export const uploadECG = async (formData, jobName, jobID) => {
const response = await axios
.post("upload_ecg.php", formData, {
headers: { "Content-Type": "multipart/formdata" },
})
.then((res) => console.log(res));
};
submitJob
const submitJob = async (e) => {
var formData = new FormData();
e.preventDefault();
// getFileContext();
formData.append("file", file);
console.log([...formData]);
const response = await uploadECG(formData, "Test Name", "Test ID");
};