asp.net-mvc – Web API模型绑定器不能与HttpPostedFileBase一起使用?
发布时间:2020-05-23 21:24:26 所属栏目:asp.Net 来源:互联网
导读:测试用于文件上载的Web API,有一个像这样的简单视图模型: public class TestModel { public string UserId {get;set;} public HttpPostedFileBase ImageFile {get;set;}} 用于方法: [HttpPost]public void Create(TestModel model) 当
|
测试用于文件上载的Web API,有一个像这样的简单视图模型: public class TestModel {
public string UserId {get;set;}
public HttpPostedFileBase ImageFile {get;set;}
}
用于方法: [HttpPost] public void Create(TestModel model) 当我尝试将多部分/表单数据编码的表单发布到操作时,我收到此异常: System.InvalidOperationException: No MediaTypeFormatter is available to read an object of type 'TestModel' from content with media type 'multipart/form-data'. at System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent content,Type type,IEnumerable`1 formatters,IFormatterLogger formatterLogger) at System.Net.Http.HttpContentExtensions.ReadAsAsync(HttpContent content,IFormatterLogger formatterLogger) at System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage request,IFormatterLogger formatterLogger) at System.Web.Http.ModelBinding.FormatterParameterBinding.ExecuteBindingAsync(ModelMetadataProvider metadataProvider,HttpActionContext actionContext,CancellationToken cancellationToken) at System.Web.Http.Controllers.HttpActionBinding.<>c__DisplayClass1.<ExecuteBindingAsync>b__0(HttpParameterBinding parameterBinder) at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext() at System.Threading.Tasks.TaskHelpers.IterateImpl(IEnumerator`1 enumerator,CancellationToken cancellationToken) 这适用于默认的MVC模型绑定器,但显然不适用于Web API.发现一些人提到你在上传文件时不能使用视图模型,只是将数据分成两个调用.这对我不起作用,因为我需要发布其他字段才能真正对上传的文件做些什么.有办法实现这个目标吗? 解决方法看我原来的答案https://stackoverflow.com/a/12603828/1171321 基本上在我的博客文章和TryValidateProperty()建议中结合我的方法来维护模型验证注释. 编辑: public class FileUpload<T>
{
private readonly string _RawValue;
public T Value { get; set; }
public string FileName { get; set; }
public string MediaType { get; set; }
public byte[] Buffer { get; set; }
public List<ValidationResult> ValidationResults = new List<ValidationResult>();
public FileUpload(byte[] buffer,string mediaType,string fileName,string value)
{
Buffer = buffer;
MediaType = mediaType;
FileName = fileName.Replace(""","");
_RawValue = value;
Value = JsonConvert.DeserializeObject<T>(_RawValue);
foreach (PropertyInfo Property in Value.GetType().GetProperties())
{
var Results = new List<ValidationResult>();
Validator.TryValidateProperty(Property.GetValue(Value),new ValidationContext(Value) {MemberName = Property.Name},Results);
ValidationResults.AddRange(Results);
}
}
public void Save(string path,int userId)
{
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
var SafeFileName = Md5Hash.GetSaltedFileName(userId,FileName);
var NewPath = Path.Combine(path,SafeFileName);
if (File.Exists(NewPath))
{
File.Delete(NewPath);
}
File.WriteAllBytes(NewPath,Buffer);
var Property = Value.GetType().GetProperty("FileName");
Property.SetValue(Value,SafeFileName,null);
}
} (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – RequireHttps导致Amazon Elastic Load Bala
- asp.net-mvc – Razor中()(括号)和{}(大括号)之间的差异
- asp.net – JObject.Parse与JsonConvert.DeserializeObject
- asp.net-mvc-3 – ASP.NET MVC3 – DateTime格式
- asp.net – 如何使用SqlDependency使OutputCache依赖于每个
- asp.net-mvc – 如何使用存储库模式处理表关系?
- asp.net – 如何防止CPU占用100%,因为iis中的工作进程
- 使用Asp.Net Identity 2在AspNetUserClaims中存储用户信息有
- 使用Gzip在ASP.NET / IIS7中输出乱码错误页面
- asp.net – 在ascx文件中使用if条件
推荐文章
站长推荐
- asp.net-core – 如何注入对特定IHostedService实
- asp.net – Context.Response.End()和Thread正在
- asp.net-mvc-3 – jQuery.validator.unobtrusive
- asp.net – 如何在网格视图列标题上添加鼠标悬停
- asp.net-mvc – Web API – 默认呈现Razor视图?
- asp.net-mvc – 人们如何使用编辑器/显示模板与H
- asp.net – MaintainScrollPositionOnPostback属
- 安装程序 – 无法安装ASP.NET MVC3 RTM?
- asp.net-mvc – 给定htmlHelper动作名称,如何找出
- asp.net-mvc – 如何在ASP.NET MVC的一个视图中使
热点阅读
