Asp.net mvc ASP.NET MVC文件上载错误-“;输入不是有效的Base-64字符串";
我正在尝试将文件上载控件添加到我的ASP.NET MVC 2表单中,但在我选择一个jpg并单击“保存”后,会出现以下错误: 输入不是有效的Base-64字符串,因为它包含一个非Base-64字符、两个以上的填充字符或填充字符中的一个非空白字符。 以下是观点:Asp.net mvc ASP.NET MVC文件上载错误-“;输入不是有效的Base-64字符串";,asp.net-mvc,Asp.net Mvc,我正在尝试将文件上载控件添加到我的ASP.NET MVC 2表单中,但在我选择一个jpg并单击“保存”后,会出现以下错误: 输入不是有效的Base-64字符串,因为它包含一个非Base-64字符、两个以上的填充字符或填充字符中的一个非空白字符。 以下是观点: <% using (Html.BeginForm("Save", "Developers", FormMethod.Post, new {enctype = "multipart/form-data"})) { %> &
<% using (Html.BeginForm("Save", "Developers", FormMethod.Post, new {enctype = "multipart/form-data"})) { %>
<%: Html.ValidationSummary(true) %>
<fieldset>
<legend>Fields</legend>
<div class="editor-label">
Login Name
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.LoginName) %>
<%: Html.ValidationMessageFor(model => model.LoginName) %>
</div>
<div class="editor-label">
Password
</div>
<div class="editor-field">
<%: Html.Password("Password") %>
<%: Html.ValidationMessageFor(model => model.Password) %>
</div>
<div class="editor-label">
First Name
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.FirstName) %>
<%: Html.ValidationMessageFor(model => model.FirstName) %>
</div>
<div class="editor-label">
Last Name
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.LastName) %>
<%: Html.ValidationMessageFor(model => model.LastName) %>
</div>
<div class="editor-label">
Photo
</div>
<div class="editor-field">
<input id="Photo" name="Photo" type="file" />
</div>
<p>
<%: Html.Hidden("DeveloperID") %>
<%: Html.Hidden("CreateDate") %>
<input type="submit" value="Save" />
</p>
</fieldset>
<% } %>
谢谢,
贾斯汀我刚刚尝试了你的代码,可以毫无问题地上传。我没有保存到数据库,我的开发人员类也没有照片属性
namespace MvcApplication5.Controllers
{
public class Developer
{
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime UpdateDate { get; set; }
public int DeveloperID { get; set; }
public string LoginName { get; set; }
public string Password { get; set; }
}
}
控制器
public class DefaultController : Controller
{
//
// GET: /Default/
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Index()
{
return View();
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Save(Developer developer)
{
//get profile photo.
var upload = Request.Files["Photo"];
if (upload.ContentLength > 0)
{
string savedFileName = Path.Combine(
@"C:\temp",
"Developer_" + developer.FirstName + "_" + developer.LastName + ".jpg");
upload.SaveAs(savedFileName);
}
developer.UpdateDate = DateTime.Now;
if (developer.DeveloperID == 0)
{//inserting new developer.
}
else
{//attaching existing developer.
}
//save changes.
//redirect to developer list.
return RedirectToAction("Index");
}
}
看法
领域
登录名
model.LoginName)%%>
model.LoginName)%%>
密码
型号(密码)%%>
名字
model.FirstName)%%>
model.FirstName)%%>
姓
model.LastName)%%>
model.LastName)%%>
照片
尽管我现在使用的是MVC3而不是MVC2,但我最近找到了一个解决方案
在保存操作中,从绑定对象中排除二进制字段,并包含一个单独的HttpPostedFileBase字段:
e、 g
请注意,您也可以这样做,以避免在视图中包含Html.Hidden元素。e、 g:
public ActionResult Save([Bind(Exclude = "Photo,DeveloperID,CreateDate")]Developer developer, HttpPostedFileBase Photo) {...}
然后,您可以直接使用此HttpPostedFileBase对象,而无需访问Request.file
就我个人而言,我实际上使用以下代码将这些类型的图像存储在数据库中的SQL“image”字段中:
if (Picture != null)
{
if (Picture.ContentLength > 0)
{
byte[] imgBinaryData = new byte[Picture.ContentLength];
int readresult = Picture.InputStream.Read(imgBinaryData, 0, Picture.ContentLength);
Developer.Picture = imgBinaryData;
}
}
希望这是有帮助的
马克我也有同样的问题。这是我找到的解决办法。 类别属性:
public byte[] Logo { get; set; }
查看代码:
@using (Html.BeginForm("StoreMyCompany", "MyCompany", FormMethod.Post, new { id = "formMyCompany", enctype = "multipart/form-data" }))
{
<div class="form-group">
@Html.LabelFor(model => model.modelMyCompany.Logo, htmlAttributes: new { @class = "control-label col-md-3" })
<div class="col-md-6">
<input type="file" name="Logo" id="fileUpload" accept=".png,.jpg,.jpeg,.gif,.tif" />
</div>
</div>
}
我刚刚从控制器的调用中排除了Logo。我也有同样的错误,但上面的解决方案对我不起作用,相反,我注意到我的模型属性名称与我随控制器传递的参数名称相同:
//POST: /Secure/Developers/Save/
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Save(Developer developer)
{
//get profile photo.
var upload = Request.Files["Photo"];
if (upload.ContentLength > 0)
{
string savedFileName = Path.Combine(
ConfigurationManager.AppSettings["FileUploadDirectory"],
"Developer_" + developer.FirstName + "_" + developer.LastName + ".jpg");
upload.SaveAs(savedFileName);
}
developer.UpdateDate = DateTime.Now;
if (developer.DeveloperID == 0)
{//inserting new developer.
DataContext.DeveloperData.Insert(developer);
}
else
{//attaching existing developer.
DataContext.DeveloperData.Attach(developer);
}
//save changes.
DataContext.SaveChanges();
//redirect to developer list.
return RedirectToAction("Index");
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult EditSOP(File file, HttpPostedFileBase Upload)
我的模型属性名称是
public byte[] Upload { get; set; }
在使用其他参数名称重命名后,它现在可以工作:
public ActionResult EditSOP(File file, HttpPostedFileBase UploadFile)
这应该不是问题,因为错误是在进入控制器操作之前抛出的,因此与保存到db无关。我不知所措b/c我认为这个错误与没有enctype=“multipart/form data”有关,我清楚地知道……实际上你给了我修复它所需要的东西。您没有Photo属性,而我有byte[]Photo属性,因此它试图将文件上载分配给它,这显然不起作用。我重命名了fileupload控件ProfilePhoto,然后它就工作了。
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult EditSOP(File file, HttpPostedFileBase Upload)
public byte[] Upload { get; set; }
public ActionResult EditSOP(File file, HttpPostedFileBase UploadFile)