Мне нужно отправить в Api Controller w/JSON (желательно) с помощью ОДНОГО запроса.
Проблема заключается в передаче данных И файла (загруженного изображения). Моя собственность подходит пустым (null).
Я просмотрел довольно много блогов, но, похоже, не смог получить данные изображения.
public class SomeModel
{
public string Name { get; set; }
public string Email { get; set; }
public string City { get; set; }
public HttpPostedFileBase Image { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public string CountryCode { get; set; }
}
[HttpPost]
public void CreateContestEntry(SomeModel model)
{
// model.Image is always null
// .. get image here - the other properties no issues
}
JQuery
// create model for controller
var model = {
Name: $.trim($contestForm.find('[name="nombre"]').val()) + ' ' + $.trim($contestForm.find('[name="apellido"]').val()),
Email: $.trim($contestForm.find('[name="email"]').val().toLowerCase()),
City: $.trim($contestForm.find('[name="cuidad"]').val()),
Title: $.trim($contestForm.find('[name="title"]').val()),
Description: $.trim($contestForm.find('[name="description"]').val()),
CountryCode: 'co',
Image: $contestForm.find('[name="file-es"]')[0].files[0] // this has the file for sure
};
$.ajax({
url: '/Umbraco/api/ControllerName/CreateContestEntry',
type: 'POST',
dataType: 'json',
data: JSON.stringify(model),
//data: $('#test-form').serialize(), // tried this and using FormData()
processData: false,
async: false,
contentType: 'application/json; charset=utf-8',
complete: function (data) {
},
error: function (response) {
console.log(response.responseText);
}
});
Блоги, на которые я смотрел:
- Загрузка файлов с добавлением данных формы в Web Api из MVC
- http://www.asp.net/web-api/overview/advanced/sending-html-form-data,-part-1
- http://www.asp.net/web-api/overview/advanced/sending-html-form-data,-part-2
- Пользовательские данные формы с несколькими файлами в API веб-API
Когда я попытался использовать FormData
и $('#form1').serialize()
, мои provider.FileData
и provider.FormData
также были пустыми. Я удалил параметр model
из метода, и точки останова попали, когда я его включил.
[HttpPost]
public void CreateContestEntry()
{
string root = HttpContext.Current.Server.MapPath("~/App_Data");
var provider = new MultipartFormDataStreamProvider(root);
try
{
// Read the form data.
Request.Content.ReadAsMultipartAsync(provider);
// This illustrates how to get the file names.
foreach (MultipartFileData file in provider.FileData)
{
// empty
}
foreach (var key in provider.FormData.AllKeys)
{
foreach (var val in provider.FormData.GetValues(key))
{
// empty
}
}
//return Request.CreateResponse(HttpStatusCode.OK);
}
catch(Exception ex)
{
}
}
РЕШЕНИЕ:
Отключение ответа @Musa, вот код Api Controller. Я сопоставил NameValueCollection с моей моделью.
[HttpPost]
public void CreateContestEntry()
{
try
{
// get variables first
NameValueCollection nvc = HttpContext.Current.Request.Form;
var model = new WAR2015ContestModel();
// iterate through and map to strongly typed model
foreach (string kvp in nvc.AllKeys)
{
PropertyInfo pi = model.GetType().GetProperty(kvp, BindingFlags.Public | BindingFlags.Instance);
if (pi != null)
{
pi.SetValue(model, nvc[kvp], null);
}
}
model.Image = HttpContext.Current.Request.Files["Image"];
}
catch(Exception ex)
{
}
}