如何在 ASP.NET C# 上接收 MultipartFormData
How to receive MultipartFormData on ASP.NET C#
我正在和一个 iOS 的人一起工作。他想通过 WebAPI ASP.NET 上传图片。我必须拨打一个可以接收这些图像的电话。
他说他正在使用 AFNetworking 通过 AFMultipartFormData
发送数据。我的问题是我怎样才能收到这个?我应该采用 JSON 格式的数据吗?或者为此需要采取什么措施?我想知道整个过程,因为这是我第一次使用 MultipartFormData
。
UPDATE
根据我使用的答案:
[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 人得到了:
Request failed: unsupported media type (415)
在网络 API 控制器中,您可以使用以下代码访问图像文件:-
HttpPostedFile xmlFile = HttpContext.Current.Request.Files[0];
如果您发布了多个文件,请将文件[0]替换为相应的计数 1 或 2 等。
然后您可以使用以下代码访问 JSON:
var fileForm = HttpContext.Current.Request.Form;
var fileKey = HttpContext.Current.Request.Form.Keys[0];
string[] jsonformat = fileForm.GetValues(fileKey);
var yourModel = JsonConvert.DeserializeObject<YourClassType>(jsonformat[0]);
如果您发布了多个 json 字符串,请将 jsonformat[0] 替换为相应的计数 1 或 2 等
我正在和一个 iOS 的人一起工作。他想通过 WebAPI ASP.NET 上传图片。我必须拨打一个可以接收这些图像的电话。
他说他正在使用 AFNetworking 通过 AFMultipartFormData
发送数据。我的问题是我怎样才能收到这个?我应该采用 JSON 格式的数据吗?或者为此需要采取什么措施?我想知道整个过程,因为这是我第一次使用 MultipartFormData
。
UPDATE
根据我使用的答案:
[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 人得到了:
Request failed: unsupported media type (415)
在网络 API 控制器中,您可以使用以下代码访问图像文件:-
HttpPostedFile xmlFile = HttpContext.Current.Request.Files[0];
如果您发布了多个文件,请将文件[0]替换为相应的计数 1 或 2 等。
然后您可以使用以下代码访问 JSON:
var fileForm = HttpContext.Current.Request.Form;
var fileKey = HttpContext.Current.Request.Form.Keys[0];
string[] jsonformat = fileForm.GetValues(fileKey);
var yourModel = JsonConvert.DeserializeObject<YourClassType>(jsonformat[0]);
如果您发布了多个 json 字符串,请将 jsonformat[0] 替换为相应的计数 1 或 2 等