In my MVC application, I am using following code to upload a file.
MODEL
public HttpPostedFileBase File { get; set; }
VIEW
@Html.TextBoxFor(m => m.File, new { type = "file" })
Everything working fine .. But I am trying to convert the result fiel to byte[] .How can i do this
CONTROLLER
public ActionResult ManagePhotos(ManagePhotos model)
{
if (ModelState.IsValid)
{
byte[] image = model.File; //Its not working .How can convert this to byte array
}
}
As Darin says, you can read from the input stream – but I’d avoid relying on all the data being available in a single go. If you’re using .NET 4 this is simple:
It’s easy enough to write the equivalent of
CopyToin .NET 3.5 if you want. The important part is that you read fromHttpPostedFileBase.InputStream.For efficient purposes you could check whether the stream returned is already a
MemoryStream: