2012-11-01 31 views
8

Tôi đang viết một ứng dụng rất nhỏ với mvc4 và khung thực thể 5.Entity Framework 5 Code đầu tiên thêm một hình ảnh

Tôi muốn thêm sản phẩm và lưu trữ và hình ảnh cho sản phẩm.

Tôi có một mô hình

[Table("CatalogItem")] 
public class CatalogItemModel 
{ 
    [Key] 
    public int CatalogItemId { get; set; } 

    public string Description { get; set; } 

    public double Price { get; set; } 

    public int ProductCount { get; set; } 

    public string Size { get; set; } 

    public string Sku { get; set; } 

    [Column(TypeName = "image")] 


    public byte[] Image { get; set; } 

    [Display(Name = "Display Catalog Item")] 
    public bool DisplayItem { get; set; } 
} 

điều khiển của tôi. Điều này không bao giờ bị tấn công.

[HttpPost] 
    public ActionResult Create(CatalogItemModel catalogitemmodel) 
    { 
     if (ModelState.IsValid) 
     { 
      db.CatalogItemModels.Add(catalogitemmodel); 
      db.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 

     return View(catalogitemmodel); 
    } 

quan điểm của tôi hình thành

<fieldset> 
    <legend>CatalogItemModel</legend> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.Description) 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.Description) 
     @Html.ValidationMessageFor(model => model.Description) 
    </div> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.Price) 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.Price) 
     @Html.ValidationMessageFor(model => model.Price) 
    </div> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.ProductCount) 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.ProductCount) 
     @Html.ValidationMessageFor(model => model.ProductCount) 
    </div> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.Size) 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.Size) 
     @Html.ValidationMessageFor(model => model.Size) 
    </div> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.Sku) 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.Sku) 
     @Html.ValidationMessageFor(model => model.Sku) 
    </div> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.DisplayItem) 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.DisplayItem) 
     @Html.ValidationMessageFor(model => model.DisplayItem) 
    </div> 
    <div class="editor-label"> 
     @Html.LabelFor(m=>m.Image) 
    </div> 

    <input name="Image" type="file"/> 
    <p> 
     <input type="submit" value="Create" /> 
    </p> 
</fieldset> 

Khi tôi cố gắng đăng một cửa hàng mới với một hình ảnh trong đầu vào tập tin của tôi tuy nhiên nó ném một lỗi

Các đầu vào không phải là một cơ sở- hợp lệ 64 chuỗi vì nó chứa ký tự 64 không phải là cơ sở, nhiều hơn hai ký tự đệm hoặc một ký tự không hợp lệ giữa các ký tự đệm.

+0

Bạn có thể thêm mã nơi bạn gán hình ảnh không? –

+0

@NickW vừa thêm hành động điều khiển. Tôi đã thêm một breakpoint tuy nhiên nó không bao giờ bị trúng –

+0

Điều gì về 'catalogitemmodel'? Làm thế nào điều này được tạo ra? Nếu breakpoint của bạn không nhận được hit thì vấn đề là sớm hơn. –

Trả lời

9

Cố gắng sửa chữa như thế:

1. Thay

<input name="Image" type="file"/> với <input name="ImageFile" type="file"/>

2. Trong bộ điều khiển:

 [HttpPost] 
     public ActionResult Create(CatalogItemModel catalogitemmodel, HttpPostedFileBase ImageFile) 
     { 
      using (var ms = new MemoryStream()) 
      { 
       ImageFile.InputStream.CopyTo(ms); 
       catalogitemmodel.Image = ms.ToArray(); 
      } 

      if (ModelState.IsValid) 
      { 
       db.CatalogItemModels.Add(catalogitemmodel); 
       db.SaveChanges(); 
       return RedirectToAction("Index"); 
      } 

      return View(catalogitemmodel); 
     } 
+0

cảm ơn @testCoder tuy nhiên ImageFile luôn được trả về là rỗng không. Tôi đã kiểm tra rằng chính tả là chính xác tuy nhiên không có niềm vui –

+0

bằng cách sử dụng var files = Request.Files Tôi đã có thể nhận được hình ảnh –

+3

DiverDan đừng quên điều này: @using (Html.BeginForm (null, null, FormMethod.Post, new { enctype = "multipart/form-data"}) – testCoder

Các vấn đề liên quan