View And Download File From Sql Db Using Entity Framework
Am new for hamdling Entity Framework.I use the following code for insert the file from fileupload button in mvc4 public ActionResult Index(NewUserModel newUser) {
Solution 1:
Assuming a basic Model of:
publicclassResume
{
publicint ResumeID {get;set;}
publicstring Name { get; set; }
publicbyte[] Resume { get;set; }
}
Store the file using:
resume.Resume = newbyte[file.ContentLength];
file.InputStream.Read(resume.Resume, 0, (file.ContentLength));
(which you are!)
To view the file you will want to return a FileContentResult.
In your view you can do something like:
@Html.ActionLink("View Resume", "ViewResume", "ResumeController", new { id = resume.ResumeID }, new { @target= "_blank" })
And the controller action will call the Action to return the file:
public FileContentResult ViewResume(int id)
{
if (id == 0) { returnnull; }
Resumeresume=newResume();
ResumeContextrc=newResumeContext();
resume = rc.Resume.Where(a => a.ResumeID == id).SingleOrDefault();
Response.AppendHeader("content-disposition", "inline; filename=file.pdf"); //this will open in a new tab.. remove if you want to open in the same tab.return File(resume.Resume, "application/pdf");
}
This is the basic method I have implemented when storing files in the DB.
Solution 2:
To view the file
view
@{
if (Model.Logo != null)
{
string imageBase64 = Convert.ToBase64String(Model.Logo);
string imageSrc = string.Format("data:image/gif;base64,{0}", imageBase64);
<imgsrc="@imageSrc"width="100"height="100" />
}
}
Post a Comment for "View And Download File From Sql Db Using Entity Framework"