当我尝试在MVC4中上传照片时,没有上传文件

时间:2013-05-23 13:23:35

标签: asp.net-mvc-4 file-upload model

事实上,我是MVC的新手,我已经开始制作新闻页面了。在此页面标题中,新闻的正文及其照片路径存储在数据库中,但照片将保存在服务器文件夹中。所以我为DB创建了一个模型:

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

        [Required(ErrorMessage = "Please enter a suitable title for your news")]
        public string title { get; set; }

        [Required(ErrorMessage = "Please enter the body news")]
        public string body { get; set; }

        public string image { get; set; }
        public DateTime dateAdded { get; set; }
    }

照片的另一个模型:

 public class UploadImageViewModel
    {
        public string Photo { get; set; }
        public HttpPostedFileBase PhotoUpload { get; set; }
    }

之后,我在我的控制器中做了一个动作:

private UploadImageViewModel model = new UploadImageViewModel();

[HttpPost]
        public ActionResult Create(NewsEntry entry)
        {
            string uploadPath = "~/images/NewsImages";

            var file = model.PhotoUpload;

            if (model.PhotoUpload.ContentLength > 0)
            {
                var fileName = Path.GetFileName(model.PhotoUpload.FileName);
                var path = Path.Combine(Server.MapPath(uploadPath), fileName);
                model.PhotoUpload.SaveAs(path);
                model.Photo = uploadPath + fileName;
                entry.image = model.Photo;
            }

            var profile = AutoMapper.Mapper.Map<NewsEntry>(model);

            var validTypes = new[] { "image/jpeg", "image/pjpeg", "image/png", "image/gif" };
            if (!validTypes.Contains(model.PhotoUpload.ContentType))
            {
                ModelState.AddModelError("Photo Upload", "Please Upload either a JPG, GIF, or PNG image.");
            }

            if (ModelState.IsValid)
            {
                entry.dateAdded = DateTime.Now;
                _db.Entries.Add(entry);
                _db.SaveChanges();
            }

            return RedirectToAction("Index");
        }

最后为此操作添加了一个视图:

@Html.ValidationSummary()

        @using(Html.BeginForm()) {
            <p>Please enter your news title: </p>
            @Html.TextBox("title")

        <br /><br />

        <p>Please enter your news body: </p>
            @Html.TextArea("body",new{rows=10,cols=45})

        <br /><br />

        <p>Please enter the source of your news: </p>
            @Html.TextBox("Source")

        <br /><br />

            <div class="editor-field">
                <label for="PhotoUpload">Upload an image for your news </label>
                @Html.TextBox("PhotoUpload", null, new { type="file" })
            </div>

        <br /><br />

        <input type="submit" value="Submit the news" >
        }

但是,没有回复的照片,而且AutoMapper也存在问题。我该怎么办? 如果有人能解决我的问题,我将不胜感激。

1 个答案:

答案 0 :(得分:1)

我认为你的问题是

  @using(Html.BeginForm())

这是在MVC应用程序中上传文件(照片或图像)的新手中常见的错误 所以只需用这个替换上面提到的那一行:

@using (Html.BeginForm("FileUpload", "Home", FormMethod.Post, 
                        new { enctype = "multipart/form-data" }))

并且,这里有一些参考资料,我相信你会帮到你:

  1. Uploading/Displaying Images in MVC 4
  2. http://www.codeproject.com/Articles/442515/Uploading-and-returning-files-in-ASP-NET-MVC
  3. In MVC4, how do I upload a file (an image) to SQL Server that's part of my domain model?