I am currently trying to upload an Image file together with a string with ajax to an Asp.Net Core Api with no luck.
I looked at: https://developer.mozilla.org/en-US/docs/Web/API/FormData/Using_FormData_Objects and thought it would be easy to send both an Image file and a string together in a FormData object, but only the Image is received.
In javascript I am appending the data and sending it with an ajax post:
let formData = new FormData();
formData.append("code", "123");
formData.append("userFile", document.querySelector("#imgInput").files[0]);
$.ajax({
async: true,
type: "post",
data: formData,
processData: false,
contentType: false,
url: "https://localhost:22333/api/postmethod",
success: function (data, response, jqXHR) {
console.log(response);
},
error: function (jqXHR, response, errorThrown) {
}
});
And in Asp.Net Core the method is:
[HttpPost]
[Route("postmethod")]
public async Task PostMethod(IFormFile code, IFormFile userFile)
I have tried setting the code
parameter to both IFormFile and string, but nothing happens. The userFile parameter is always set correctly to the Image file, which makes me wonder what I am doing wrong when uploading the code
string.
TLDR: Can't upload both a image file and string with ajax call to Asp.Net Core 3.1 Api.
Is there a better way to upload the files and parameter?