模型绑定 HttpPostedFileBase 然后将文件存储到数据存储
ASP.NET MVC 似乎可以正确地自动绑定 HTML 表单的文件输入字段和 HttpPostedFileBase。另一方面,它无法从文件输入字段绑定到字节数组。我尝试过,但它发出异常 - 无法转换为 Base64。之前我的 Model 类上只有字节数组属性,因为后来我需要它来将对象序列化为 XML 文件。
现在我想出了这个解决方法,它工作正常,但我不确定这是否可以:
[DataContract]
public class Section : BaseContentObject
{
...
[DataMember]
public byte[] ImageBytes;
private HttpPostedFileBase _imageFile;
public HttpPostedFileBase ImageFile
{
get { return _imageFile; }
set
{
_imageFile = value;
if (value.ContentLength > 0)
{
byte[] buffer = new byte[value.ContentLength];
value.InputStream.Read(buffer, 0, value.ContentLength);
ImageBytes = buffer;
ImageType = value.ContentType;
}
}
}
[DataMember]
public string ImageType { get; set; }
}
ASP.NET MVC seems to correctly automatically bind between HTML form's file input field and HttpPostedFileBase. On the other hand it cannot bind from file input field to byte array..I tried and it issues exception - something about not being able to convert to Base64. I had only the byte array property on my Model classes previously because later on I need it to perform serialization of the object into XML file.
Now I've come up with this workaround and it works fine but I am not sure if this will be ok:
[DataContract]
public class Section : BaseContentObject
{
...
[DataMember]
public byte[] ImageBytes;
private HttpPostedFileBase _imageFile;
public HttpPostedFileBase ImageFile
{
get { return _imageFile; }
set
{
_imageFile = value;
if (value.ContentLength > 0)
{
byte[] buffer = new byte[value.ContentLength];
value.InputStream.Read(buffer, 0, value.ContentLength);
ImageBytes = buffer;
ImageType = value.ContentType;
}
}
}
[DataMember]
public string ImageType { get; set; }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你正在让你的模型与你的控制器紧密连接。通常的方法是:
在这种情况下,您的模型不需要了解
HttpPostedFileBase
,这严格来说是一个 ASP.NET 概念。如果您需要超出
DefaultModelBinder
提供的功能(很多)的复杂绑定,通常的方法是在Global.asax
中注册专门的 ModelBinder,然后接受您自己的 Model 类作为操作方法参数,如下所示:在
Global.asax
中:例如,此 ModelBinder 可以查找随表单发布的任何文件,并将该文件的内容绑定到
Data< /code> 你的
事物
的属性。在您的控制器中:
在此操作方法中,您的 ThingModelBinder 将处理所有绑定,使其对控制器和模型都是透明的。
在这种情况下,无需修改实际的 Model 类以了解 ASP.NET 并使用 ASP.NET 进行操作。毕竟,您的模型类应该代表您的实际数据。
I think you are letting your Model connect to closely with your Controller. The usual way to do this is:
In this case, there is no need for your Model to be aware of
HttpPostedFileBase
, which is strictly an ASP.NET concept.If you need complex binding beyond what the
DefaultModelBinder
supplies (which is alot), the usual way is to register specialized ModelBinders inGlobal.asax
and then accept your own Model classes as Action Method arguments, like so:In
Global.asax
:This ModelBinder could then, for example, find any file that was posted with the form and bind the contents of that file to the
Data
property of yourThing
.And in your Controller:
In this Action Method, your
ThingModelBinder
would have handled all binding, making it transparent to both the Controller and the Model.Modifying your actual Model classes to be aware of, and function with, ASP.NET would not be necessary in this case. Your Model classes are, after all, supposed to represent your actual data.
显然,MVC Futures 2 中有巨大的变化(刚刚发现),特别是在 Model Binder 方面。
例如,我的输入文件绑定到字节数组的问题,现在有一个绑定器:
• BinaryDataModelBinderProvider – 处理将base-64 编码输入绑定到byte[] 和System.Linq.Data.Binary 模型。
Apparently there are huge changes (just found it out) in MVC Futures 2, especially regarding Model Binders.
For instance, the problem with my input file binding to byte array, there is a binder now:
• BinaryDataModelBinderProvider – Handles binding base-64 encoded input to byte[] and System.Linq.Data.Binary models.