是否有可能在上传的文件在asp.net webapi模型绑定? [英] is it possible to have modelbinding in asp.net webapi with uploaded file?
问题描述
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ {组; }
public string FileName {get;组; }
public HttpPostedFileBase File {get;组;
控制器:
public void Post(UploadFileModel模型)
{
//永远不会到达...
}
我得到一个错误
No MediaTypeFormatter is可以从介质类型为multipart / form-data的内容中读取UploadFileModel类型的对象。
有没有解决方案
这不是很容易 的可能。 Web API中的模型绑定与MVC中的模型绑定基本上是不同的,您必须编写一个MediaTypeFormatter,将文件流读入您的模型,并绑定可能相当具有挑战性的原语。
最简单的解决方案是使用某种类型的 MultipartStreamProvider
和其他参数使用 FormData
名称值收集关闭该提供程序
示例 - http://www.asp.net/web-api/overview/working-with-http/sending-html-form- data-part-2 :
$ b $ pre $ public async Task< HttpResponseMessage> PostFormData()
{
if(!Request.Content.IsMimeMultipartContent())
{
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
}
string root = HttpContext.Current.Server.MapPath(〜/ App_Data);
var provider = new MultipartFormDataStreamProvider(root);
尝试
{
await Request.Content.ReadAsMultipartAsync(provider);
//显示所有的键值对。
foreach(var key in provider.FormData.AllKeys)
{
foreach(var val in provider.FormData.GetValues(key))
{
Trace.WriteLine (string.Format({0}:{1},key,val));
}
}
返回Request.CreateResponse(HttpStatusCode.OK);
catch(System.Exception e)
{
return Request.CreateErrorResponse(HttpStatusCode.InternalServerError,e);
}
}
the model:
public class UploadFileModel
{
public int Id { get; set; }
public string FileName { get; set; }
public HttpPostedFileBase File { get; set; }
}
the controller:
public void Post(UploadFileModel model)
{
// never arrives...
}
I am getting an error
"No MediaTypeFormatter is available to read an object of type 'UploadFileModel' from content with media type 'multipart/form-data'."
Is there anyway around this?
It's not easily possible. Model binding in Web API is fundamentally different than in MVC and you would have to write a MediaTypeFormatter that would read the stream of files into your model and additionally bind primitives which can be considerably challenging.
The easiest solution is to grab the file stream off the request using some type of MultipartStreamProvider
and the other parameters using FormData
name value collection off that provider
Example - http://www.asp.net/web-api/overview/working-with-http/sending-html-form-data,-part-2:
public async Task<HttpResponseMessage> PostFormData()
{
if (!Request.Content.IsMimeMultipartContent())
{
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
}
string root = HttpContext.Current.Server.MapPath("~/App_Data");
var provider = new MultipartFormDataStreamProvider(root);
try
{
await Request.Content.ReadAsMultipartAsync(provider);
// Show all the key-value pairs.
foreach (var key in provider.FormData.AllKeys)
{
foreach (var val in provider.FormData.GetValues(key))
{
Trace.WriteLine(string.Format("{0}: {1}", key, val));
}
}
return Request.CreateResponse(HttpStatusCode.OK);
}
catch (System.Exception e)
{
return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
}
}
这篇关于是否有可能在上传的文件在asp.net webapi模型绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!