The
FormData
object needs to contain the entire data to submit.
You'll also need to set the
processData
and
contentType
options to
false
, and remove the
dataType: 'json'
option.
And you should properly encode your user's name before emitting it into a Javascript variable. Since this appears to be ASP.NET Core, you should be able to use
the JsonHelper
extensions[
^]:
let formData = new FormData();
formData.append("username", @Json.Serialize(User.Identity.Name));
var content = $("#content").val();
formData.append("content", content);
var file = document.querySelector("#myFile").files[0];
formData.append("image", file);
$.ajax({
url:'/Member/Tweet/Add',
data: formData
type: 'POST',
processData: false,
contentType: false,
Using FormData Objects - Web APIs | MDN[
^]
Sending FormData with jQuery.ajax() | Matt Lunn[
^]