将文件存储到数据库时发生错误
本文关键字:错误 数据库 文件 存储 | 更新日期: 2023-09-27 18:16:36
上传文件时出错输入不是一个有效的Base-64字符串,因为它包含一个非Base-64字符、两个以上的填充字符,或者填充字符中有一个非法字符。我的分配DB属性>> 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");
}
<<h1> 视图/strong> <% 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; }
代替
<%: Html.TextBoxFor(model => model.FileLocation, new { type="file"})%>
取简单的HTML标签为:
<input type="file" name="file" id="file"/>