I am trying to upload a picture, send the picture by the front end (axios request), and then this error is returned by the server.
Cannot deserialize the current JSON array into type 'Microsoft.AspNetCore.Http.IFormFile' because the type requires a JSON object to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. Path 'file', line 1, position 339."
[HttpPost("add")]
public async Task<IActionResult> Add(Post post, IFormFile file){............}
this is my axios request
const submit = useCallback(
async (values: PostExtensions) => {
debugger;
setLoading(true);
const tarih = dayjs(values.date);
values.tarih = dayjs(date, "YYYY-MM-DDTHH:mm:ss Z").format();
const formdata = new FormData();
formdata.append("postId", values.postId!);
formdata.append("file", values.file![0]);
formdata.append("userId", values.userId!);
formdata.append("projectId", values.projectId!);
formdata.append("date", values.date!);
await entitySubmit({
values: JSON.parse(JSON.stringify(values)),
dispatch: dispatch,
returndata: true,
headers: {
"Content-Type": "multipart/form-data"
},
links: [
{
type: 0,
link: "post/addpost",
actionType: ActionType.add_Post,
method: "POST",
},
{
type: 1,
link: "post/editpost",
actionType: ActionType.edit_Post,
method: "POST",
},
],
id: "postId",
});
return null;
},
[show, dispatch]
);
when I tried to post formdata, It does'nt submit.
EDIT 1: I found the problem where is, formData send null file object something like this
formdata.get('file') // '[Object object]'