【问题标题】:ASP MVC Image Upload Error “The input is not a valid Base-64 string”ASP MVC 图像上传错误“输入不是有效的 Base-64 字符串”
【发布时间】:2016-02-13 21:32:21
【问题描述】:

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

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

永远无法到达控制器,因为我一上传图片就会发生错误。我不知道该怎么办。任何帮助表示赞赏!

@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-mvc base64 image-upload


    【解决方案1】:

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

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

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

    【讨论】:

    • 我已经用控制器更新了 OP。我一直在考虑你的建议,但不幸的是它并没有改变任何东西。如果我在控制器中设置断点,它永远不会被命中。
    • 这是因为模型绑定程序在实际到达您的操作代码之前生成了错误。因为您在Component 上有一个名为Image 的属性,所以modelbinder 会尝试将数据绑定到那里,这就是导致错误的原因。事实上,你有另一个名为 image 的参数永远不会发挥作用。您需要为输入(和参数)使用与模型上的任何名称不同的名称。
    • 我在模型中添加了一个 HttpPostedFileBase 属性,它消除了错误并允许我上传文件。这里的下一步行动是什么?将 HttpPostedFileBase 参数转换为 byte[] 并存储它?
    • 如果“模型”是你的实体,你不应该有 HttpPostedFileBase 属性。要么使用视图模型,要么将其绑定到您之前尝试执行的附加参数。但是,是的,一旦您拥有发布的文件,您需要从流中读取字节并使用它在您的模型上设置适当的属性。
    【解决方案2】:

    public HttpPostedFileBase image { get; set; } 在你的模型上使用它 &lt;input type="file" name="image" /&gt; 在您的视图名称上使用它的道具应该与 HttpPostedFileBase 图像相同,并在 post 方法中捕获它,然后将其转换为字节

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多