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.

2
  • 1
    Have you checked this? stackoverflow.com/questions/2288163/… Commented Feb 20, 2013 at 19:14
  • Also, please note that "please help me out with sample codes" is guaranteed to get a poor reception on this site. Commented Feb 20, 2013 at 19:25

2 Answers 2

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");
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the help -Floremin. This gives me a kick start. By the way What is the UploadedImages in db.UploadedImages.Add(img); ?
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.
Got it Floremin. Thanks for your time.
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?
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: stackoverflow.com/questions/4736155/…
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.