2013-10-04 13 views
16

więc mam projekt, który jest koszyk, muszę zapisywać obrazy w bazie danych zamiast umieszczania ich na serwerze, tu jest mój modelJak zapisać obraz w bazie danych przy użyciu MVC 4

namespace ShoppingCart.Models 
{ 
    [Bind(Exclude = "ItemID")] 
    public class Item 
    { 
     [ScaffoldColumn(false)] 
     public int ItemID { get; set; } 
     [DisplayName("Category")] 
     public int CategoryID { get; set; } 
     [DisplayName("Brand")] 
     public int BrandID { get; set; } 
     [Required(ErrorMessage = "A Name is required")] 
     [StringLength(160)] 
     public string Title { get; set; } 
     public string Description { get; set; } 
     [Required(ErrorMessage = "Price is required")] 
     [Range(0.01, 100.00, 
      ErrorMessage = "Price must be between 0.01 and 500.00")] 
     public decimal Price { get; set; } 
     [DisplayName("Album Art URL")] 
     [StringLength(1024)] 

     public string ItemArtUrl { get; set; } 
     public byte[] Picture { get; set; } 

     public virtual Category Category { get; set; } 
     public virtual Brand Brand { get; set; } 
     public virtual List<OrderDetail> OrderDetails { get; set; } 
    } 
} 

Więc jestem pewien, w jaki sposób przejść do kontrolera, aby wstawić obrazy lub widoku, aby je wyświetlić, Mam wyszukiwania informacji na ten temat, ale nie mogę naprawdę znaleźć niczego, Im przy użyciu kodu kodu podmiotu w pierwszej kolejności.

Odpowiedz

36

Istnieją dwa proste sposoby wykonywania zdjęć - jeden jest po prostu wrócić sam obraz w kontrolerze:

[HttpGet] 
    [AllowAnonymous] 
    public ActionResult ViewImage(int id) 
    { 
     var item = _shoppingCartRepository.GetItem(id); 
     byte[] buffer = item.Picture; 
     return File(buffer, "image/jpg", string.Format("{0}.jpg", id)); 
    } 

I widok po prostu odwoływać się do niej:

<img src="Home/ViewImage/10" /> 

Dodatkowo można je umieścić w ViewModel:

viewModel.ImageToShow = Convert.ToBase64String(item.Picture); 

iw widoku:

@Html.Raw("<img src=\"data:image/jpeg;base64," + viewModel.ImageToShow + "\" />"); 

Dla magazynu danych wystarczy użyć tablicy bajtów (varbinary(max)) lub obiektu typu blob lub dowolnego zgodnego typu.

Przesyłanie zdjęć

Tutaj, przedmiot zwany HeaderImage jest EntityFramework EntityObject. Kontroler będzie wyglądać następująco:

[HttpPost] 
    public ActionResult UploadImages(HttpPostedFileBase[] uploadImages) 
    { 
     if (uploadImages.Count() <= 1) 
     { 
      return RedirectToAction("BrowseImages"); 
     } 

     foreach (var image in uploadImages) 
     { 
      if (image.ContentLength > 0) 
      { 
       byte[] imageData = null; 
       using (var binaryReader = new BinaryReader(image.InputStream)) 
       { 
        imageData = binaryReader.ReadBytes(image.ContentLength); 
       } 
       var headerImage = new HeaderImage 
       { 
        ImageData = imageData, 
        ImageName = image.FileName, 
        IsActive = true 
       }; 
       imageRepository.AddHeaderImage(headerImage); 
      } 
     } 
     return RedirectToAction("BrowseImages"); 
    } 

The View będzie wyglądać następująco:

  @using (Html.BeginForm("UploadImages", "Home", FormMethod.Post, new { enctype = "multipart/form-data" })) 
      { 
       <div class="row"> 
        <span class="span4"> 
         <input type="file" name="uploadImages" multiple="multiple" class="input-files"/> 
        </span> 
        <span class="span2"> 
         <input type="submit" name="button" value="Upload" class="btn btn-upload" /> 
        </span> 
       </div> 
      } 
Powiązane problemy