如何在mvc WEB API 2中上传文件和模型参数。
我有以下代码,如果我从动作中移除模型,它可以正常工作,但是对于模型,我收到以下错误。
" message":"请求实体的媒体类型' multipart / form-data'是 不支持此资源。"," exception_message":"否 MediaTypeFormatter可用于读取类型的对象 ' CreateTicketDTO'来自媒体类型的内容 '多部分/格式数据'",
[HttpPost]
[Route("api/support/tickets/")]
public async Task<HttpResponseMessage> Insert(CreateTicketDTO dto)
{
if(dto == null)
return Request.CreateResponse(HttpStatusCode.BadRequest, "Please supply required parameters");
var provider = new MultipartMemoryStreamProvider();
var a = await Request.Content.ReadAsMultipartAsync(provider);
foreach (var file in provider.Contents)
{
var filename = file.Headers.ContentDisposition.FileName.Trim('\"');
var buffer = await file.ReadAsByteArrayAsync();
//Do whatever you want with filename and its binaray data.
}
using (_ticketService)
{
var ticket = await _ticketService.CreateNewTicket(dto);
return Request.CreateResponse(HttpStatusCode.OK, ticket);
}
}
我正在Postman中创建一个帖子请求。