2012-06-17 13 views
6

MVC4 uygulamasında fotoğrafları yüklemek için bir denetleyici oluşturmaya çalışıyorum. Ama bu hatayı almaya devam ediyorum. Giriş, temel olmayan bir 64 karakter, ikiden fazla dolgu karakteri veya dolgu karakterleri arasında beyaz olmayan bir boşluk karakteri içerdiğinden geçerli bir Base-64 dizesi değildir.Fotoğrafı MVC 4 Uygulamalarına Yükleyin

PhotosController.cs

public class PhotoController : Controller 
    { 
     public ActionResult Index() 
     { 
      using (var ctx = new BlogContext()) 
      { 
       return View(ctx.Photos.AsEnumerable()); 
      } 
     } 

     public ActionResult Upload() 
     { 
      return View(new Photo()); 
     } 

     [HttpPost] 
     public ActionResult Upload(PhotoViewModel model) 
     { 
      var photo = Mapper.Map<PhotoViewModel, Photo>(model); 
      if (ModelState.IsValid) 
      { 
       PhotoRepository.Save(photo); 
       return RedirectToAction("Index"); 
      } 
      return View(photo); 
     } 
    } 

Photo.cs

public class Photo 
    { 
    public int Id { get; set; } 

    public Byte[] File { get; set; } 

    public string Name { get; set; } 

    public string Description { get; set; } 

    public string AlternateText { get; set; } 
    } 

PhotoViewModel.cs

public class PhotoViewModel 
    { 
     public int Id { get; set; } 

     public HttpPostedFileBase File { get; set; } 

     public string Name { get; set; } 

     public string Description { get; set; } 

     public string AlternateText { get; set; } 
    } 

/Photos/Upload.cshtml

@model Rubish.Models.Photo 

    @{ 
     ViewBag.Title = "Upload"; 
    } 

    <h2>Upload</h2> 

    @using (Html.BeginForm("Upload","Photo",FormMethod.Post,new {enctype="multipart/form-data"})) { 
     @Html.ValidationSummary(true) 

     <fieldset> 
      <legend>Photo</legend> 

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

      <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"> 
       <label for="file">FileName:</label> 
      </div> 
      <div class="editor-field"> 
       <input name="File" id="File" type="file"/> 
      </div> 
      <p> 
       <input type="submit" value="Create" /> 
      </p> 
     </fieldset> 
    } 

    <div> 
     @Html.ActionLink("Back to List", "Index") 
    </div> 

    @Scripts.Render("~/bundles/jqueryval") 

PhotoRepository

public class PhotoRepository 
    { 
     private static BlogContext _ctx; 

     public PhotoRepository() 
     { 
      _ctx = new BlogContext(); 
     } 

     public static void Save(Photo p) 
     { 
      _ctx.Photos.Add(p); 
      _ctx.SaveChanges(); 
     } 
    } 

cevap

15

sorun tip byte[] taşımaktadır File adında görünümü modelinde bir özelliği vardır ve ayrıca tip HttpPostedFileBase ait file denilen bir işlem parametresini kullanarak olmasıdır. Sorun şu ki, model bağlayıcısı, byte[] modelinizde bir özellikle karşılaştığında, değerini base64 kullanarak istek değerinden bağlamaya çalışır. Bu istek dışında, yüklenen dosyanın multipart/form-data kodlanmış bir değerine sahip olursunuz ve bir istisna alırsınız.

public class PhotoViewModel 
{ 
    public HttpPostedFileBase File { get; set; } 

    ... other properties 
} 

ve denetleyici eylem şimdi olacak:

[HttpPost] 
public ActionResult Upload(PhotoViewModel model) 
{ 
    if (ModelState.IsValid) 
    { 
     // map the domain model from the view model that the action 
     // now takes as parameter 
     // I would recommend you AutoMapper for that purpose 
     Photo photo = ... 

     // Pass the domain model to a DAL layer for processing 
     Repository.Save(photo); 

     return RedirectToAction("Index"); 
    } 
    return View(photo); 
} 

fakir yolu

Bunu düzeltmek için doğru yolu görünümleri üzere alan modelleri geçirmeden yerine görünüm modellerini kullanmaktır

<input name="PhotoFile" id="File" type="file"/> 
: ve tamamen modeli cilt kandırmak için dosya girişini yeniden adlandırmak için benim tarafımdan tavsiye edilmezve denetleyici eyleminiz:
[HttpPost] 
public ActionResult Upload(Photo photo, HttpPostedFileBase photoFile) 
{ 
    ... 
} 
+0

Otomatik belirleyiciyi kullanmaya çalışma dışında çalıştığınız için teşekkürler. 'Automapper: Eksik TipMap yapılandırması veya desteklenmeyen eşleştirmeyi' atar. Kod yukarıda güncellendi –