使用实体FrameWork从sql数据库查看和下载文件 [英] view and download File from sql db using Entity FrameWork
问题描述
对于实体框架,我使用以下代码从fileupload按钮插入文件
public ActionResult索引(NewUserModel newUser)
{
恢复newuserResume = new Resume();
if(Request!= null)
{
HttpPostedFileBase file = Request.Files [UploadedFile];
if((file!= null)&(file.ContentLength> 0)&!string.IsNullOrEmpty(file.FileName))
{
string fileName = file.FileName;
string fileextn = Path.GetExtension(fileName);
if(fileextn ==.pdf|| fileextn ==.doc)
{
string fileContentType = file.ContentType;
byte [] fileBytes = new byte [file.ContentLength];
file.InputStream.Read(fileBytes,0,Convert.ToInt32(file.ContentLength));
newuserResume.Resumes = fileBytes;
Hrentitymodel.Resumes.Add(newuserResume);
Hrentitymodel.SaveChanges();
}
else {
ViewBag.FileExtn =文件应为.doc或.pdf格式;
}
}
}
return View(Index);
}
它将正常工作,这意味着使用Varbinary(max)存储在DB中的文件格式。
现在,如何在MVC4中使用实体框架在sql数据库中查看和下载文件
假设一个基本的模型:
public class Resume
{
public int ResumeID {get ; set;}
public string Name {get;组; }
public byte []恢复{get; set;
}
使用以下方式存储文件:
resume.Resume = new byte [file.ContentLength];
file.InputStream.Read(resume.Resume,0,(file.ContentLength));
(你是!)
要查看文件,您将要返回一个FileContentResult。
在您的视图中,您可以执行以下操作:
@ Html.ActionLink(View Resume,ViewResume,ResumeController,new {id = resume.ResumeID},new {@ target =_blank})
控制器操作将调用Action来返回文件:
public FileContentResult ViewResume(int id)
{
if(id == 0){return null; }
简历resume = new Resume();
ResumeContext rc = new ResumeContext();
resume = rc.Resume.Where(a => a.ResumeID == id).SingleOrDefault();
Response.AppendHeader(content-disposition,inline; filename = file.pdf); //这将打开一个新的选项卡..删除如果你想打开在同一个选项卡。
return File(resume.Resume,application / pdf);
}
这是在DB中存储文件时实现的基本方法。 / p>
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)
{
Resume newuserResume = new Resume();
if (Request != null)
{
HttpPostedFileBase file = Request.Files["UploadedFile"];
if ((file != null) && (file.ContentLength > 0) && !string.IsNullOrEmpty(file.FileName))
{
string fileName = file.FileName;
string fileextn = Path.GetExtension(fileName);
if (fileextn == ".pdf" || fileextn == ".doc")
{
string fileContentType = file.ContentType;
byte[] fileBytes = new byte[file.ContentLength];
file.InputStream.Read(fileBytes, 0, Convert.ToInt32(file.ContentLength));
newuserResume.Resumes = fileBytes;
Hrentitymodel.Resumes.Add(newuserResume);
Hrentitymodel.SaveChanges();
}
else {
ViewBag.FileExtn = "File Should be in .doc or .pdf Format";
}
}
}
return View("Index");
}
It will working fine which means the file stored in DB with Varbinary(max) Format. Now,How to view and download the file from sql db using entity framework in MVC4
Assuming a basic Model of:
public class Resume
{
public int ResumeID {get;set;}
public string Name { get; set; }
public byte[] Resume { get;set; }
}
Store the file using:
resume.Resume = new byte[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) { return null; }
Resume resume = new Resume();
ResumeContext rc = new ResumeContext();
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.
这篇关于使用实体FrameWork从sql数据库查看和下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!