在尝试上传图像时,获取“输入不是有效的Base-64字符串,因为它包含非基本64字符”

时间:2017-01-05 22:34:03

标签: c# asp.net-mvc entity-framework

我不知道为什么我会得到这个例外。我正在尝试实现一个简单的上传图片功能。

当我尝试将图像与其余数据一起保存时,我收到错误。

我怀疑问题出在model,因为我在另一个项目中使用了actionpublic class Company { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int CompanyId { get; set; } public byte[] ImageData { get; set; } [NotMapped] public HttpPostedFileBase UploadImage { get; set; } [NotMapped] public string ImageBase64 => System.Convert.ToBase64String(ImageData); public string CompanyName { get; set; } public string CompanyAddress { get; set; } public string CompanyCountry { get; set; } public string CompanyCity { get; set; } public string CompanyPostalCode { get; set; } public string CompanyPhoneNumber { get; set; } public string CAId { get; set; } } 方法中的代码,代码也运行了。

有人可以帮忙解决这个问题。我相信我很亲密。

型号:

public ActionResult Create([Bind(Include = "CompId,ImageData,CompanyName,CompanyAddress,CompanyCountry,CompanyCity,CompanyPostalCode,CompanyPhoneNumber,EmailCA")] Company company, HttpPostedFileBase UploadImage)
    {
        if (ModelState.IsValid)
        {
            byte[] buf = new byte[UploadImage.ContentLength];
            UploadImage.InputStream.Read(buf, 0, buf.Length);
            company.ImageData = buf;

            db.Companies.Add(company);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(company);
    }

控制器:

@Html.AntiForgeryToken()

<div class="form-horizontal">
    <h4>Company</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.ImageData, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            <div class="col-md-10">
                @Html.TextBoxFor(model => model.ImageData, new { type = "file" })
                @*<input type="file" name="ImageData" class="input-files" />*@
            </div>

        </div>
    </div>

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

    <div class="form-group">
        @Html.LabelFor(model => model.CAId, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.CAId, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.CAId, "", 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>

查看:

NMAMapView

1 个答案:

答案 0 :(得分:0)

在您看来,您使用@Html.TextBoxFor(model => model.ImageData, new { type = "file" }),我认为您应该使用@Html.TextBoxFor(model => model.UploadImage, new { type = "file" })

为了进行该传递,我认为您需要将UploadImage属性添加到Bind(Include)字符串,如下所示:

public ActionResult Create([Bind(Include = "CompId,ImageData,CompanyName,CompanyAddress,CompanyCountry,CompanyCity,CompanyPostalCode,CompanyPhoneNumber,EmailCA,UploadImage")] Company company)