I'm using ASP.NET MVC and Entity Framework code-first approach. I want to upload a file (.doc or .pdf) to a SQL Server. I have searched and browsed through many forums but couldn't find a definite solution for uploading a file to SQL Server using Entity Framework code-first approach.

I have written the following code to upload a file but am getting an error while saving the file to database.

Here is the following code

The controller is :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using FileuploadEF.Models;
namespace FileuploadEF.Controllers
{
    public class HomeController : Controller
    {
        FileEntities db = new FileEntities();

        public ActionResult Index()
        {
             return View();
        }

        public ActionResult About()
        {
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult upload()
        {
            foreach (string file in Request.Files)
            {
                var PostedFile = Request.Files[file];
                string filetype = Request.Files[file].ContentType;
                int filelength = Request.Files[file].ContentLength;
                Stream filestream = Request.Files[file].InputStream;
                byte[] filedata = new byte[filelength];
                string filename= Path.GetFileName(Request.Files[file].FileName);
                filestream.Read(filedata, 0, filelength);
                var data = new FileDump
                {
                    FileName = filename,
                    FileType = filetype,
                    FileContent = filedata
                };

                  db.FileDumps.Add(data);

                db.SaveChanges();
            }

            return View();
        }

    }
}

The class filedump for creating database is :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace FinaleuploadEF.Models
{
    public class FileDump
    {
        public int Id { get; set; }
        public string FileName { get; set; }
        public string FileType { get; set; }
        public byte[] FileContent { get; set; }
    }
}

The entities class is :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;

namespace FinaleuploadEF.Models
{
    public class FinalEntities : DbContext
    {
        public DbSet<FileDump> FileDumps { get; set; }

    }
}

The connection string:

 <connectionStrings>
      <add name="FinalEntities"
       connectionString="Data Source=|DataDirectory|FinaleuploadEF.sdf"
       providerName="System.Data.SqlServerCe.4.0"/>
    </connectionStrings>

I am getting the following error while saving a file to the database:

www.freeimagehosting.net/kmwts

Can anyone please suggest a solution for the above problem? Or any changes required in the above code?

share|improve this question
1  
Read Download and Upload images from/to SQL Server using ASP.NET MVC - using EF for this makes no sense at all - just use "straight" ADO.NET code for this task as shown in that blog post – marc_s Jun 23 at 11:12
1  
Put a try catch in you code and see and check the EntityValidationErros property, so you can get more clear picture of actual error. – Furqan Jun 23 at 11:15
feedback

1 Answer

You should try to isolate the problem.

Try first running the code without saving the contents of the file.

If that works extract the code that saves the data, and write a unit test that saves a byte array that you build in code.

If that works write a unit test that saves a byte array that you build by reading a file from disk.

If that works try using your code to save the uploaded file to disk.

You should be able to identify the root cause of the problem by doing the above.

share|improve this answer
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.