如何解决ASP.NET 无效的 base64 输入输入不是有效的 Base-64 字符串
我尝试将照片上传到我的控制器,但它显示此错误。但是,其他页面工作正常。我可以问为什么吗?下面是我的查看代码。
@using (Html.BeginForm("ManageProfile","Owner",FormMethod.Post,new { enctype = "multipart/form-data" }))
{
<input type="file" id="profilePic" name="profilePic" />
}
另一个工作正常的视图
<div class="form-group">
@Html.LabelFor(model => model.photo3,htmlAttributes: new { @class = "control-label col-md-4" })
<div class="col-md-8">
<input type="file" id="photoupload3" name="photoupload3" />
<img id="photo3" style="max-height:300px;max-width:300px;" />
</div>
</div>
[HttpPost]
public ActionResult ManageProfile(Owner owner,HttpPostedFileBase profilePic)
{
string name = HttpContext.User.Identity.Name;
var dbOwner = _context.OwnerDB.Find(owner.ownerID);
dbOwner.contact = owner.contact;
dbOwner.email = owner.email;
dbOwner.Name = owner.Name;
dbOwner.password = owner.password;
if (profilePic != null)
{
dbOwner.profilePic = new byte[profilePic.ContentLength];
profilePic.InputStream.Read(dbOwner.profilePic,profilePic.ContentLength);
}
_context.SaveChanges();
return RedirectToAction("Index","Home",null);
}
另一个控制器工作正常
[HttpPost]
public ActionResult NewRoom(Room room,HttpPostedFileBase photoupload,HttpPostedFileBase photoupload2,HttpPostedFileBase photoupload3)
{
var ownerName = HttpContext.User.Identity.Name.ToString();
Owner owner = _context.OwnerDB.Where(o => o.Name == ownerName).FirstOrDefault();
room.ownerID = owner.ownerID;
if (photoupload != null)
{
room.photo1 = new byte[photoupload.ContentLength];
photoupload.InputStream.Read(room.photo1,photoupload.ContentLength);
}
堆栈跟踪 stacktrace
查看完整代码
@using (Html.BeginForm("ManageProfile",new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Owner</h4>
<hr />
@Html.ValidationSummary(true,"",new { @class = "text-danger" })
@Html.HiddenFor(model => model.ownerID)
<div class="form-group">
@Html.LabelFor(model => model.Name,htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name,new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name,new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.contact,htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.contact,new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.contact,new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.email,htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.email,new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.email,new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.password,htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.password,new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.password,new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@*@Html.LabelFor(model => model.profilePic,htmlAttributes: new { @class = "control-label col-md-2" })*@
<div class="col-md-10">
<div class="col-md-10">
@{
if (Model.profilePic != null)
{
var base64 = Convert.ToBase64String(Model.profilePic);
var imgsrc = string.Format("data:image/gif;base64,{0}",base64);
<img src='@imgsrc' width="300" height="300" class="reel"
data-image='@imgsrc'
data-stitched="600"
data-frames="30"
data-frame="15"
data-rows="1"
data-row="1"
data-loops="false"
alt="@Model.Name" />
}
}
<input type="file" id="profilePic" name="profilePic" />
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。