asp.net-mvc - ASP MVC 图片上传错误 “The input is not a valid Base-64 string”

标签 asp.net-mvc base64 image-upload

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

“输入不是有效的 Base-64 字符串,因为它包含非 Base-64 字符、两个以上的填充字符或填充字符中包含非法字符。”

永远无法到达 Controller ,因为我上传图像后就会发生错误。我不知道该怎么办。如有任何帮助,我们将不胜感激!

@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>
}

更新:

这是创建 Controller :

    [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);
    }

最佳答案

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

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

您已将文件输入绑定(bind)到 Image,我的猜测是 Image 是您模型上的字节数组。您不能直接发布到字节数组。您需要绑定(bind)到 HttpPostedFileBase 类型的属性,然后您可以从中读取字节数组并将其设置到其他属性上。

关于asp.net-mvc - ASP MVC 图片上传错误 “The input is not a valid Base-64 string”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33673477/

相关文章:

asp.net-mvc - MVC 和 Entity Framework 何时处置实体对象

javascript - 确定 razor 生成 View 中 html 表的大小

C - 调用函数时遇到问题

java - Base64 编码与 Ascii85 编码

javascript - 在react js中预览并上传同一组件中的两个单独的图像

ASP.NET MVC 1.0 AfterBuilding View 在 TFS 构建上失败

javascript - jCrop HTML5 Canvas Base64

asp.net-mvc - 使用模型绑定(bind)在 MVC3 中上传多个文件

javascript - TinyMCE 图片上传