Saturday, July 9, 2011

File Uploads of ASP .NET MVC saving image in database

[HttpPost]
        public ActionResult PartialPhotoDetail(int hotelID, HttpPostedFileBase hotelPhoto) {

            if (hotelPhoto.ContentLength > 0) {
                byte[] fileBytes = new byte[hotelPhoto.ContentLength];
                int byteCount = hotelPhoto.InputStream.Read(fileBytes, 0, hotelPhoto.ContentLength);

                AccommodationService.InsertHotelPhoto(new Photo { HotelID = hotelID, photo = fileBytes });
            }

            return RedirectToAction("Accommodation", new { id = hotelID, tab = 4 });
        }

public ActionResult GetHotelPhoto(int id) {
            var hotelPhoto = AccommodationService.GetHotelPhotoById(id);
            return new FileContentResult(hotelPhoto.photo.ToArray(), "image/png");
        }

No comments:

Post a Comment