首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将照片上传到MVC 4应用程序

将照片上传到MVC 4应用程序
EN

Stack Overflow用户
提问于 2012-06-17 13:23:32
回答 1查看 14.1K关注 0票数 6

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

PhotosController.cs

代码语言:javascript
复制
 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

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
 @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

代码语言:javascript
复制
public class PhotoRepository 
    {
        private static BlogContext _ctx;

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

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

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-06-17 14:23:37

问题是,您的视图模型中有一个名为File的属性,它的类型为byte[],并且您还使用了一个名为fileHttpPostedFileBase类型的操作参数。问题是,当模型绑定器遇到byte[]类型的模型上的属性时,它会尝试使用base64从请求值绑定它的值。除了在请求中你有一个上传文件的multipart/form-data编码值,并且你得到了一个异常。

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

代码语言:javascript
复制
public class PhotoViewModel
{
    public HttpPostedFileBase File { get; set; }

    ... other properties
}

而控制器操作现在将变为:

代码语言:javascript
复制
[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);
}

我完全不推荐的糟糕方法是重命名您的文件输入以欺骗模型绑定器:

代码语言:javascript
复制
<input name="PhotoFile" id="File" type="file"/>

和你的控制器动作:

代码语言:javascript
复制
[HttpPost]
public ActionResult Upload(Photo photo, HttpPostedFileBase photoFile)
{
    ...
}
票数 15
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11069107

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档