We want to send an image file as multipart/form to the backend, we try to use html form to get file and send the file as formData, here are the codes
export
For sending multipart/formdata
, you need to avoid contentType
, since the browser automatically assigns the boundary
and Content-Type
.
In your case by using fetch
, even if you avoid Content-Type
it sets to default text/plain
. So try with jQuery ajax
. which removes the contentType
if we set it to false.
This is the working code
var data = new FormData();
var imagedata = document.querySelector('input[type="file"]').files[0];
data.append("data", imagedata);
$.ajax({
method: "POST",
url: fullUrl,
data: data,
dataType: 'json',
cache: false,
processData: false,
contentType: false
}).done((data) => {
//resolve(data);
}).fail((err) => {
//console.log("errorrr for file upload", err);
//reject(err);
});