ASP MVC 图像上传错误“输入不是有效的 Base-64 字符串”

ASP MVC Image Upload Error “The input is not a valid Base-64 string”

我有这个表格,我正在尝试上传图片。当我单击提交按钮时,出现以下错误:

"The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters."

永远无法访问控制器,因为我一上传图片就发生了错误。我不知所措。感谢您的帮助!

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

    <div class="form-group">
        <div class="col-md-10">
            @Html.LabelFor(model => model.ComponentModel.Image, htmlAttributes: new {@class = "control-label col-md-2"})
            <a class="btn" href="javascript:;">
                Choose File...
                <input type="file" name="Image" Size="40" style="position: absolute; z-index: 2; top: 0; left: 0; filter: alpha(opacity=0); opacity: 0; background-color: transparent; color: transparent"
                       onchange='$("#upload-file-info").html($(this).val());'/>
            </a>
            <span class="label label-info" id="upload-file-info"></span>
        </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>
}

更新:

这是创建控制器:

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Create(Component component, HttpPostedFileBase image = null)
    {
        if (ModelState.IsValid)
        {
            if (image != null)
            {
                component.Image = new byte[image.ContentLength];
                image.InputStream.Read(component.Image, 0, image.ContentLength);
            }
            componentRepository.InsertComponent(component);
            componentRepository.Save();
            return RedirectToAction("Index", "Home");
        }
        return RedirectToAction("Index", component);
    }

你这里确实没有提供足够的信息,但我将根据错误消息进行大胆猜测。

你说它没有击中你的控制器,但它有点必须击中你的控制器。错误来自 ASP.NET,因此它会返回到服务器。

您已将文件输入绑定到 Image,我猜 Image 是您模型中的一个字节数组。您不能 post 直接指向字节数组。您需要绑定到类型 HttpPostedFileBase 的 属性,然后您可以从中读取字节数组并将其设置到另一个 属性.

public HttpPostedFileBase image { get; set; } 在你的模型上使用它 <input type="file" name="image" /> 在你的 View name prop 上使用它应该与 HttpPostedFileBase 图像相同,并在 post 方法上捕获它,然后将其转换为 byte