为什么我无法上传.docx
word文档?我可以上传其他文件,例如.mp3
,.doc
和.txt
。
错误如下:
用户代码未处理System.Data.Entity.Validation.DbEntityValidationException 的HResult = -2146232032 消息=一个或多个实体的验证失败。有关详细信息,请参阅“EntityValidationErrors”属性。 来源=的EntityFramework 堆栈跟踪: 在System.Data.Entity.Internal.InternalContext.SaveChanges() 在System.Data.Entity.Internal.LazyInternalContext.SaveChanges() 在System.Data.Entity.DbContext.SaveChanges() at Elearning_Assignment.Views.OnAssess.Student.OnAssess_MainPage_S.btnUpload_Click(Object sender,EventArgs e)位于c:\ Users \ James \ Desktop \ WAD Assignment \ Elearning_Assignment \ Elearning_Assignment \ Views \ OnAssess \ Student \ OnAssess_MainPage_S.aspx.cs:第53行 在System.Web.UI.WebControls.Button.OnClick(EventArgs e) 在System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) 在System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) 在System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl,String eventArgument) 在System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) 在System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint,Boolean includeStagesAfterAsyncPoint) InnerException:
我的代码如下。
protected void btnUpload_Click(object sender, EventArgs e)
{
int i = 0;
HttpPostedFile file = FileUpload1.PostedFile;
BinaryReader br = new BinaryReader(file.InputStream);
byte[] buffer = br.ReadBytes(file.ContentLength);
if (FileUpload1.HasFile)
{
string fileets = Path.GetExtension(FileUpload1.FileName);
if (fileets.ToLower() != ".docx" && fileets.ToLower() != ".doc")
{
Response.Write("Only Allow .docx/.doc");
}
else
{
using (ELearningDatabaseEntities db = new ELearningDatabaseEntities())
{
db.OnlineAssessmentDocuments.Add(
new OnlineAssessmentDocument
{
DocID = "DOC00001",
DocName = file.FileName,
ContentType = file.ContentType,
DocSize = file.ContentLength,
DocContent = buffer,
LearnerID = "LEN00001"
}
);
db.SaveChanges();
}
}
}
}
答案 0 :(得分:0)
您遇到了验证错误。试试这段代码(取自this question),看看错误是什么:
try
{
db.SaveChanges();
}
catch (DbEntityValidationException dbEx)
{
foreach (var validationErrors in dbEx.EntityValidationErrors)
{
foreach (var validationError in validationErrors.ValidationErrors)
{
Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
}
}
}
一旦您知道验证错误是什么,请更正它。