我在上传文件时遇到错误,我已经尝试谷歌了解同样的问题,但未能得到任何正确的解决方案。请帮我。 我附加了我的代码和错误。 我正在使用asp.net 2015和sql server management 2008 我的数据库表看起来像这样
P_Id int 标题varchar(50) 类别varchar(50) 描述varchar(50) 档案图片(50)
This is the image of my error.
这是我的控制器
[HttpPost]
public ActionResult FileUpload(Project pro, HttpPostedFileBase file)
{
bool Status = false;
String message = "";
MyProjectsEntities db = new MyProjectsEntities();
var file = Request.Files[0];
try
{
if (file != null && file.ContentLength > 0)
{
var content = new byte[file.ContentLength];
file.InputStream.Read(content, 0, file.ContentLength);
pro.Files = content;
db.Projects.Add(pro);
db.SaveChanges();
message = "Success";
Status = true;
}
}
catch (DbEntityValidationException e)
{
foreach (var eve in e.EntityValidationErrors)
{
message = " Entity of type \"{0}\" in state \"{1}\" has the following validation errors:" + eve.Entry.Entity.GetType().Name + " " + eve.Entry.State;
//Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
// eve.Entry.Entity.GetType().Name, eve.Entry.State);
foreach (var ve in eve.ValidationErrors)
{
message = "- Property: \"{0}\", Error: \"{1}\"" + ve.PropertyName + " " + ve.ErrorMessage;
//Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
// ve.PropertyName, ve.ErrorMessage);
}
}
//throw;
}
ViewBag.Message = message;
ViewBag.Status = Status;
return RedirectToAction("FileUpload");
}
这是我的模特
using System;
using System.Collections.Generic;
namespace MyProjects.Models
{
public partial class Project
{
public int P_ID { get; set; }
public string Title { get; set; }
public string Category { get; set; }
public string Description { get; set; }
public byte[] Files { get; set; }
}}
这是我的观点
@model MyProjects.Models.Project
@{
ViewBag.Title = "FileUpload";
}
<h2>FileUpload</h2>
@if (ViewBag.Status != null && Convert.ToBoolean(ViewBag.Status))
{
if (ViewBag.Message != null)
{
<div class="alert alert-success">
<strong>Success! </strong>@ViewBag.Message
</div>
}
}
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Project</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Title, htmlAttributes: new { @class =
"control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Title, new { htmlAttributes = new
{ @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Title, "", new {
@class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Category, htmlAttributes: new { @class
= "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Category, new { htmlAttributes =
new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Category, "", new {
@class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Description, htmlAttributes: new {
@class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Description, new { htmlAttributes
= new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Description, "", new {
@class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Files, htmlAttributes: new { @class =
"control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.Files, "", new { @type = "file",
@multiple = "multiple" })
@Html.ValidationMessageFor(model => model.Files, "", new {
@class = "text-danger" })
</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>
</div>
if (ViewBag.Message != null)
{
<div class="alert alert-danger">
<strong>Error! </strong>@ViewBag.Message
</div>
}
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
这是我的视图模型
namespace MyProjects.Models
{
[MetadataType(typeof(ProjectMetadata))]
public partial class Project
{
}
public class ProjectMetadata
{
[Required(ErrorMessage = "The {0} field is required!")]
public string Title { get; set; }
[Required(ErrorMessage = "The {0} field is required!")]
public string Category { get; set; }
[Required(ErrorMessage = "The {0} field is required!")]
public string Description { get; set; }
[Required(ErrorMessage = "The {0} field is required!")]
[DataType(DataType.Upload)]
public IEnumerable<HttpPostedFileBase> Files { get; set; }
}
}