【发布时间】: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