模型绑定 HttpPostedFileBase 然后将文件存储到数据存储

发布于 2024-08-27 19:11:29 字数 877 浏览 3 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

荒岛晴空 2024-09-03 19:11:29

我认为你正在让你的模型与你的控制器紧密连接。通常的方法是:

public ActionResult AcceptFile(HttpPostedFileBase submittedFile) {
  var bytes = submittedFile.FileContents;
  var model = new DatabaseThing { data = bytes };
  model.SaveToDatabase();
}

在这种情况下,您的模型不需要了解 HttpPostedFileBase,这严格来说是一个 ASP.NET 概念。

如果您需要超出 DefaultModelBinder 提供的功能(很多)的复杂绑定,通常的方法是在 Global.asax 中注册专门的 ModelBinder,然后接受您自己的 Model 类作为操作方法参数,如下所示:

Global.asax 中:

ModelBinders.Binders.Add(typeof(MyThing), new ThingModelBinder()); 

例如,此 ModelBinder 可以查找随表单发布的任何文件,并将该文件的内容绑定到 Data< /code> 你的事物的属性。

在您的控制器中:

public ActionResult AcceptThing(MyThing thing) {
  thing.Data.SaveToDatabase();
}

在此操作方法中,您的 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:

public ActionResult AcceptFile(HttpPostedFileBase submittedFile) {
  var bytes = submittedFile.FileContents;
  var model = new DatabaseThing { data = bytes };
  model.SaveToDatabase();
}

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 in Global.asax and then accept your own Model classes as Action Method arguments, like so:

In Global.asax:

ModelBinders.Binders.Add(typeof(MyThing), new ThingModelBinder()); 

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 your Thing.

And in your Controller:

public ActionResult AcceptThing(MyThing thing) {
  thing.Data.SaveToDatabase();
}

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.

眉黛浅 2024-09-03 19:11:29

显然,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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文