【发布时间】:2017-03-15 15:38:04
【问题描述】:
我正在和一个 iOS 人一起工作。他想通过 WebAPI ASP.NET 上传图片。我必须拨打一个可以接收这些图像的电话。
他说他正在使用 AFNetworking 通过AFMultipartFormData 发送数据。我的问题是,我怎样才能在我的最后收到这个?我应该以 JSON 格式获取数据吗?或者为此需要采取哪些措施?我想知道整个过程,因为这是我第一次使用 MultipartFormData。
更新
根据我使用的答案:
[HttpPut]
public IHttpActionResult GetPatientFilesAction(int id, Model.Patients.PatientFiles patientFile)
{
Model.Patients.PatientFiles pFile=new Model.Patients.PatientFiles();
try
{
HttpPostedFile xmlFile = HttpContext.Current.Request.Files[0];
var fileForm = HttpContext.Current.Request.Form;
var fileKey = HttpContext.Current.Request.Form.Keys[0];
string[] jsonformat = fileForm.GetValues(fileKey);
pFile = Newtonsoft.Json.JsonConvert.DeserializeObject<Model.Patients.PatientFiles>(jsonformat[0]);
}
catch (Exception ex)
{
pFile.ErrorMessage = ex.ToString();
}
return Ok(pFile);
}
但是 iOS 家伙得到了:
请求失败:不支持的媒体类型 (415)
【问题讨论】:
-
您是在编写接收 Web API 控制器吗?还是要调用他们现有的 Web Api 端点?
-
就是这样……首先上传图片……上传完之后剩下的 JSON 对象将来自 IOS 应用程序。
-
你可以在同一个HTTP请求中同时发送图片和json数据。
标签: c# asp.net json multipartform-data multipart