【问题标题】:Upload Photo To MVC 4 Applications将照片上传到 MVC 4 应用程序
【发布时间】:2012-06-19 14:40:54
【问题描述】:

我正在尝试创建一个控制器以在我的 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);
        }
    }

照片.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")

照片库

public class PhotoRepository 
    {
        private static BlogContext _ctx;

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

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

【问题讨论】:

    标签: asp.net-mvc file-upload view controller


    【解决方案1】:

    问题是您的视图模型中有一个名为File 的属性,它的类型为byte[],并且您还使用了一个名为fileHttpPostedFileBase 类型的操作参数。问题在于,当模型绑定器在您的模型上遇到 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)
    {
        ...
    }
    

    【讨论】:

    • 谢谢它似乎在工作,除非尝试使用自动映射器。它抛出“Automapper:缺少 TypeMap 配置或不支持的映射”。代码在上面更新了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-11
    • 2010-09-12
    • 2012-11-21
    • 1970-01-01
    • 1970-01-01
    • 2012-02-17
    • 1970-01-01
    相关资源
    最近更新 更多