I want to use the post API to send an excel file as a blob file. Below is my code.
const Csvsubmit = (event) => {
console.log("csv", excelFile);
setLoadingcsv(true);
const requestOption = {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
file_upload: excelFile,
created_by: user.id,
}),
};
const response = fetch(
process.env.REACT_APP_BACKEND + subdomain + "/staff/csvupload/",
requestOption
).then(
(res) => {
if (res.status === 201) {
res.json().then((json) => {
setLoadingcsv(false);
console.log(json);
setopenid(0);
});
} else if (res.status === 406) {
console.log("file format is not supported");
setLoadingcsv(false);
} else {
setLoadingcsv(false);
res.json().then((json) => {
setLoadingcsv(false);
});
}
},
(error) => {
console.log(error);
}
);
console.log("Answer", requestOption.body);
};
The "csv" cansole blob data is printed, but the body is empty.how to send this file(.xls,.xslx,.csv)
Json doesn't support binary data. You need to encode the blob into something else, (base64 is recomended), Good Luck!
See also this answer,