Asp.net mvc 将照片上载到MVC4应用程序

Asp.net mvc 将照片上载到MVC4应用程序,asp.net-mvc,file-upload,view,controller,Asp.net Mvc,File Upload,View,Controller,我正在尝试创建一个控制器,以便在MVC4应用程序中上载照片。但我一直在犯这个错误。输入不是有效的Base-64字符串,因为它包含非Base-64字符、两个以上的填充字符或填充字符中的非空白字符 PhotosController.cs public class PhotoController : Controller { public ActionResult Index() { using (var ctx = new BlogC

我正在尝试创建一个控制器,以便在MVC4应用程序中上载照片。但我一直在犯这个错误。输入不是有效的Base-64字符串,因为它包含非Base-64字符、两个以上的填充字符或填充字符中的非空白字符

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);
        }
    }
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; }
    }
/照片/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")

问题在于,您的视图模型中有一个名为
File
的属性,该属性的类型为
byte[]
,您还使用了一个名为
File
的操作参数,类型为
HttpPostedFileBase
。问题在于,当模型绑定器在您的模型上遇到类型为
byte[]
的属性时,它会尝试使用base64从请求值绑定其值。除了在请求中有一个上传文件的
multipart/form data
encoded值之外,还有一个异常

解决此问题的正确方法是使用视图模型,而不是将域模型传递给视图:

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"/>

谢谢,除了尝试使用automapper时,它似乎正在工作。它抛出“自动映射:缺少类型映射配置或不支持的映射”。以上代码已更新
[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)
{
    ...
}