简体   繁体   English

使用.Net MVC3和Entity Framework将文件存储在SQL Server数据库中

[英]Store file in SQL Server database using .Net MVC3 with Entity Framework

I want to store and retrieve a file into/from SQL Server. 我想在SQL Server中存储文件或从中检索文件。

My infrastructure is: 我的基础架构是:

  • SQL Server 2008 SQL Server 2008
  • C# .Net MVC3 with Entity Framework. 具有实体框架的C#.Net MVC3。

Please help me out with datatypes that I have to use on SQL Server and C# file. 请帮我解决我必须在SQL Server和C#文件上使用的数据类型。 If possible to store file without refreshing the page that would be more closer to my requirement. 如果可以在不刷新页面的情况下存储文件,那将更符合我的要求。

Thanks. 谢谢。

Here's some "sample codes" ;) I omitted bunch of declarations, validation, etc. so the code will not run as is, but you should be able to get the idea. 这是一些“示例代码”;)我省略了一些声明,验证等,因此该代码不会按原样运行,但是您应该能够理解。 Use ajax type request to submit your file form if you don't want to refresh the page. 如果您不想刷新页面,请使用ajax类型请求提交文件表单。

// model
public class UploadedImage
{
    public int UploadedImageID { get; set; }
    public string ContentType { get; set; }
    public byte[] File { get; set; }
}

// controller
public ActionResult Create()
{
    HttpPostedFileBase file = Request.Files["ImageFile"];

    if (file.ContentLength != 0)
    {
        UploadedImage img = new UploadedImage();
        img.ContentType = file.ContentType;
        img.File = new byte[file.ContentLength];

        file.InputStream.Read(img.File, 0, file.ContentLength);

        db.UploadedImages.Add(img);
        db.SaveChanges();
    }

    return View();
}

ActionResult Show(int id) 
{
    var image = db.UploadedImages.Find(id);
    if (image != null)
    {
        return File(image.File, image.ContentType, "filename goes here");
    }
}

Sql Datatype is image SQL数据类型是图像

Here is a great article that tells how to do this. 这是一篇很棒的文章,告诉您如何执行此操作。

http://www.hanselman.com/blog/ABackToBasicsCaseStudyImplementingHTTPFileUploadWithASPNETMVCIncludingTestsAndMocks.aspx http://www.hanselman.com/blog/ABackToBasicsCaseStudyImplementingHTTPFileUploadWithASPNETMVCIncludingTestsAndMocks.aspx

Good luck. 祝好运。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用ASP.NET MVC中的Entity Framework将多张图片上传到SQL服务器数据库 - Upload multiple pictures to SQL Server database using Entity Framework in ASP.NET MVC 使用ASP.NET MVC中的Entity Framework将图片上传到SQL服务器数据库 - Upload picture to SQL Server database using Entity Framework in ASP.NET MVC 从访客创建客户 - 使用 MVC3、C#、实体框架、Razor 视图、SQL 服务器 - Create a Customer from a Guest - using MVC3, C#, Entity Framework, Razor Views, SQL Server 如何使用ASP.NET MVC中的实体框架在SQL Server中保存业务模型实体 - How to save a business model entity in SQL Server using Entity Framework in ASP.NET MVC 如何在不使用实体框架的情况下使用ASP NET MVC5从SQL Server显示表? - How to display table from SQL Server using ASP NET MVC5 without using Entity Framework? 使用实体框架在SQL Server数据库中存储空字符串而不是Null - Store empty string instead of Null in SQL Server database using Entity Framework 从MVC .net应用程序使用Entity Framework连接到SQL数据库时出错 - Error connecting to SQL database with Entity Framework from MVC .net application 使用LinqToCsv将CSV文件写入Sql数据库-.NET MVC 4-实体框架 - Writing CSV files into Sql database using LinqToCsv - .NET MVC 4 - Entity Framework 如何在 MVC3 和实体框架上使用 Code First 将另一个表添加到我的数据库中? - How do I add another table to my database using Code First on MVC3 and Entity Framework? 使用实体框架在SQL Server中存储对象列表 - Store list of object in SQL Server using Entity Framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM