我有一个使用Entity Framework和Microsoft SQL Server 2008构建的ASP.NET MVC解决方案。 我需要创建一个允许用户上传文件的功能。
我想要的是:
- 一个使用Entity Framework将文件存储在数据库中的解决方案
- 一个可以通过某种哈希/校验和检测并防止重复上传相同文件的解决方案
- 关于数据库/表设计的提示
我有一个使用Entity Framework和Microsoft SQL Server 2008构建的ASP.NET MVC解决方案。 我需要创建一个允许用户上传文件的功能。
我想要的是:
byte[]
属性。将上传文件的内容分配给实体对象的该属性,并在ObjectContext
中保存更改。MD5CryptoServiceProvider
类。ID 标题 路径 摘要 上传日期 --- ----- -------- ---------------- ----------- 1 TestPodcast /Podcasts/ep1.mp3 一个测试播客 2010-02-12
路径
存储了播客物理位置的引用。我使用了 Scott Hanselman 在ASP.NET MVC文件上传中的基础案例研究,包括测试和模拟 来处理文件上传部分。public void AddDocument(HttpPostedFileBase file)
{
try
{
using (TransactionScope scope = new TransactionScope())
{
try
{
using (var ctx = new Entities())
{
EntityDoc doc = new EntityDoc(); //The document table
doc.DocumentFileName = file.FileName; //The file Name
using (var reader = new System.IO.BinaryReader(file.InputStream))
{
doc.DocumentFile = reader.ReadBytes(file.ContentLength); // the Byte [] Field
}
ctx.EntityDocs.Add(doc);
ctx.SaveChanges();
scope.Complete();
}
}
catch (Exception ex)
{
throw ex;
}
}
}
catch (Exception ex)
{
throw ex;
}
}