I have the following function:
public class UserRequest
{
public string RequestMessage { get; set; }
public HttpPostedFileBase Photo { get; set; }
}
public void CreateRequest(UserRequest userRequest)
{
}
When I post to this function with the following script the posted image is always null:
var requestMessage = document.getElementById("txtReqMessage").value;
var inputFile = document.getElementById("reqphoto");
var imageFile = inputFile.files[0];
$.ajax({
type: "POST",
url: "/UserRequest/CreateRequest/",
contentType: 'application/json; charset=utf-8',
data: JSON.stringify({
"RequestMessage": requestMessage,
"Photo": imageFile
}),
success: function () {
alert("Success");
}
I read about posting complex data so I changed the script to:
var requestMessage = document.getElementById("txtReqMessage").value;
var inputFile = document.getElementById("reqphoto");
var imageFile = inputFile.files[0];
var formData = new FormData();
formData.append("RequestMessage", requestMessage);
formData.append("Photo", imageFile);
$.ajax({
type: "POST",
url: "/UserRequest/CreateRequest/",
data: formData,
dataType: 'json',
contentType: false,
processData: false,
success: function () {
alert("Success");
}
});
But now something is wrong I get Error 500.