2012-06-17 8 views
6

MVC4アプリケーションで写真をアップロードするコントローラを作成しようとしています。しかし、私はこのエラーが発生し続けます。入力は、基本64以外の文字、2つ以上のパディング文字、またはパディング文字の中の非空白文字を含むため、有効なBase-64文字列ではありません。写真をMVC 4アプリケーションにアップロード

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(); 
     } 
    } 

答えて

15

問題は、あなたがタイプbyte[]であるFileと呼ばれるビューモデルのプロパティを持っていて、また、タイプHttpPostedFileBasefileと呼ばれるアクションパラメータを使用していることです。問題は、モデルバインダーがモデルbyte[]のモデルでプロパティーを検出すると、その値をbase64を使用して要求値からバインドしようとすることです。要求の中にアップロードされたファイルのmultipart/form-dataエンコードされた値があることを除いて例外が発生します。

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

    ... other properties 
} 

とコントローラのアクションが今になります:

[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); 
} 

貧しい方法

この問題を解決する正しい方法は、ビューにドメインモデルを渡すのではなく、ビューモデルを使用することですあなたがファイルの名前を変更して、モデルバインダーの名前を変えることはまったくお勧めできません:

<input name="PhotoFile" id="File" type="file"/> 

とあなたのコントローラーアクション:

[HttpPost] 
public ActionResult Upload(Photo photo, HttpPostedFileBase photoFile) 
{ 
    ... 
} 
+0

ありがとうございますautomapperを使用しようとしている以外は動作しているようです。 'Automapper:Missing TypeMapの設定またはサポートされていないマッピング'がスローされます。コードは上記で更新されています –

関連する問題