[asp.net-mvc]当上传图片到数据库在 MVC C# System.Web.HttpPostedFileWrapper 错误

发布时间: 2017/3/25 2:57:01
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我这里有一个问题。 当我删除("创造","DriverRegs",FormMethod.Post,新 {enctype ="多部分/格式数据"})从我的 BeginForm,图像路径存储在数据库上正常情况下,但当我把它上面背到 BeginForm,然后图像文件存储在我的文件夹,但在数据库上出现此错误System.Web.HttpPostedFileWrapper。 错误代码是什么? 如果有人能帮我,我会感激。 在此先感谢。

这里是我的代码︰

控制器

public ActionResult Create(DriverReg model, HttpPostedFileBase file)
        {
            if (ModelState.IsValid)
            {
                if (file != null && file.ContentLength > 0)
                {
                    var fileName = Path.GetFileName(file.FileName);
                    var phisicalPath = Path.Combine(Server.MapPath("~/Content/uploads/"), fileName);
                    file.SaveAs(phisicalPath);
                    DriverReg newRecord = new DriverReg();
                    newRecord.FullName = model.FullName;
                    newRecord.Address = model.Address;
                    newRecord.Postcode = model.Postcode;
                    newRecord.Contact = model.Contact;
                    newRecord.Email = model.Email;
                    newRecord.County = model.County;
                    newRecord.File = phisicalPath;
                }
                db.DriverRegs.Add(model);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(model);
        }

模型

 public class DriverReg
    {
        [Key]
        public int DrvId { get; set; }
        public string FullName { get; set; }
        public string Address { get; set; }
        public string Postcode { get; set; }
        public string Contact { get; set; }
        public string Email { get; set; }
        public string County { get; set; }
        [DataType(DataType.Upload)]
        [Display(Name = "Upload Files")]
        [Required(ErrorMessage = "Please choose file to upload")]
        public string File { get; set; }
        public DateTime Date { get; set; }


        internal int UploadImageInDataBase(HttpPostedFileBase file, DriverRegViewModel model)
        {
            throw new NotImplementedException();
        }

    }
    public class DriverDbContext : DbContext
    {
        public DriverDbContext()
            : base("VanRemovals")
        {
        }

    public static DriverDbContext Create()
        {
            return new DriverDbContext();
        } 

          public DbSet<DriverReg> DriverRegs { get; set; }
    }

视图

@using (Html.BeginForm ("创造"、"DriverRegs",FormMethod.Post,新 {enctype ="多部分/格式数据"})) {@Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>DriverReg</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.FullName, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.FullName, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.FullName, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Address, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Address, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Address, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Postcode, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Postcode, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Postcode, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Contact, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Contact, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Contact, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.County, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.County, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.County, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.File, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                <input type="file" class="file-input" name="file" />
                @Html.ValidationMessageFor(model => model.File, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Date, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Date, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Date, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

解决方法 1:

这是跟你开玩笑的微小细节,所以进行更改,如与评论

  db.DriverRegs.Add(newRecord);//change this part
                db.SaveChanges();

编辑

首先我不知道它是如何工作的好,当你删除位你但然后你想要保存不是 DriveReg model 因为这是你在做什么。相反,您想要保存DriverReg newRecord

当窗体发送到控制器它发送文件作为 System.Web.HttpPostedFileWrapper 所以如果你保存 DriverReg model 这就是你得在你的数据库中。到那时你还没有做一些逻辑去 phisicalPath 当你表现出你想要 DriverReg newRecord 。自两个 newRecordmodel 是同一类型的 DriverReg 看起来好像你要保存正确的数据,但你不是。

官方微信
官方QQ群
31647020