【发布时间】:2014-08-06 15:01:37
【问题描述】:
当我尝试上传文件时发生错误 输入不是有效的 Base-64 字符串,因为它包含非 base-64 字符、两个以上的填充字符或填充字符中的非法字符。 我的分配数据库属性>> FileLocation 是一个 varBinary(MAX) , 我做错了哪一部分?
控制器
[HttpPost]
public ActionResult Create(Assignment assignment)
{
if (Request.Files != null && Request.Files.Count == 1)
{
var file = Request.Files[0];
if (file != null && file.ContentLength > 0)
{
var content = new byte[file.ContentLength];
file.InputStream.Read(content, 0, file.ContentLength);
assignment.FileLocation = content;
// the rest of your db code here
}
}
assignment.SubmissionDate = DateTime.Now;
db.Assignments.Add(assignment);
db.SaveChanges();
return RedirectToAction("Index");
}
查看
<% using (Html.BeginForm("Create", "Assignment", FormMethod.Post, new { enctype = "multipart/form-data" }))
{ %>
<%: Html.ValidationSummary(true) %>
...........
<div class="editor-label">
<%: Html.LabelFor(model => model.FileLocation) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.FileLocation, new { type="file"})%>
<%: Html.ValidationMessageFor(model => model.FileLocation) %>
</div>
......
型号
public string AssignmentID { get; set; }
public Nullable<System.DateTime> SubmissionDate { get; set; }
public string Status { get; set; }
//[Range(0,100, ErrorMessage="Only Value between 0-100 is accepted.")]
public Nullable<decimal> Mark { get; set; }
public string Comments { get; set; }
public byte[] FileLocation { get; set; }
【问题讨论】:
-
@WeakestInCoding...试试下面的答案,如果问题仍然存在,它会起作用,然后评论...thankzz
-
你成功了,谢谢=D
-
很高兴它成功了,谢谢...
-
抱歉回复晚了,非常感谢!!!!!!!!!!!!!!!!!!!!!!!! =D
标签: c# asp.net asp.net-mvc asp.net-mvc-4 httppostedfilebase