0

I want to store and retrieve a file into/from SQL Server.

My infrastructure is:

  • SQL Server 2008
  • C# .Net MVC3 with Entity Framework.

Please help me out with datatypes that I have to use on SQL Server and C# file. If possible to store file without refreshing the page that would be more closer to my requirement.

Thanks.

Vinay
  • 87
  • 1
  • 7
  • 1
    Have you checked this? http://stackoverflow.com/questions/2288163/save-files-in-database-with-entity-framework – aspiring Feb 20 '13 at 19:14
  • Also, please note that "please help me out with sample codes" is guaranteed to get a poor reception on this site. – GalacticCowboy Feb 20 '13 at 19:25

2 Answers2

4

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.

// 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");
    }
}
Floremin
  • 3,969
  • 15
  • 20
  • Thanks for the help -Floremin. This gives me a kick start. By the way What is the UploadedImages in db.UploadedImages.Add(img); ? – Vinay Feb 20 '13 at 20:50
  • db is your database context, defined in your controller. d.UploadedImages is a collection that holds all objects of UploadedImage type. Basically it represents a database table which EF would create to store UploadedImage records. – Floremin Feb 20 '13 at 20:54
  • Got it Floremin. Thanks for your time. – Vinay Feb 20 '13 at 21:15
  • Hi, I am using it in Web Forms app with Entity Framework, Able to store file in DB. Now I want to read it in a stream for processing it through the service. I really don't want to store it on hard disk before opening it with a Stream. Is it possible to open directly this file from database through a Stream? – user2739418 May 12 '15 at 12:11
  • Do you really need it as a Stream? `image.File` is a `byte` array, could you use that? If not, here's how to create stream: http://stackoverflow.com/questions/4736155/how-do-i-convert-byte-to-stream-in-c – Floremin May 12 '15 at 14:41
0

Sql Datatype is image

Here is a great article that tells how to do this.

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

Good luck.

DavidEdwards
  • 593
  • 3
  • 12