This is not a good way to get the image from the view. Use
HttpPostedFileBase[
^].
An example would be-
Controller:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Upload(PhotoForSingleItem photo)
{
PhotoViewImage newImage = new PhotoViewImage();
HttpPostedFileBase file = Request.Files["OriginalLocation"];
newImage.Name = photo.Name;
newImage.Alt = photo.AlternateText;
newImage.ContentType = file.ContentType;
Int32 length = file.ContentLength;
byte[] tempImage = new byte[length];
file.InputStream.Read(tempImage, 0, length);
newImage.ActualImage = tempImage ;
newImage.Save();
return View();
}
View:
<form method="post" enctype="multipart/form-data" action="Photo/Upload">
<div>
<span>
Name:
</span>
<span>
<input type="text" id="Name" name="Name" />
</span>
</div>
<div>
<span>
Alternate Text:
</span>
<span>
<input type="text" id="AlternateText" name="AlternateText" />
</span>
</div>
<div>
<span>
Image
</span>
<span>
<input type="file" id="OriginalLocation" name="OriginalLocation" />
</span>
</div>
<div>
<input type="submit" value="Upload" />
</div>
</form>
See the complete tutorial here-
ASP.Net MVC: Upload Image to Database and Show Image “Dynamically” Using a View[
^]
-KR