Search code examples
c#asp.netasp.net-mvcimagehttppostedfilebase

Should I convert image into HttpPostedFileBase?


I need to post a form. One of the properties of my model is an image.

To do so I am using in fact 2 properties:

  • Logo is a Byte[] and is used to show the image in display mode.
  • LogoForPost is a HttpPostedFileBase and is used to upload image in edit mode.

I can create easily. I can display easily. My problem is to modify.

When I enter modify mode, and immediately press the submit button, I get an understandable (and predictable) message telling me that the LogoForPost property is required.

I could solve this problem easily by converting Logo into LogoForPost, but I guess it must be complicated to convert an image into an HttpPostedFileBase. In fact, there are multiple articles on StackOverFlow explaining how to convert an HttpPostedFileBase into a Byte[], but not the other way round. This makes me think that perhaps I don't need to, and that there might be a better way to solve the problem.


Solution

  • I realised (with the help of Yaugen Vlasau) that it would be a lot easier to simply remove the required attribute depending on whether I am on edit mode (remove) or create mode (mantain required attribute).