【问题标题】:view and download File from sql db using Entity FrameWork使用实体框架从 sql db 查看和下载文件
【发布时间】:2014-07-12 09:21:44
【问题描述】:

我是 hamdling Entity Framework 的新手。我使用以下代码从 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");
        }

它可以正常工作,这意味着文件以 Varbinary(max) 格式存储在 DB 中。 现在,如何在MVC4中使用实体框架从sql db查看和下载文件

【问题讨论】:

    标签: c# sql entity-framework asp.net-mvc-4 entity-framework-4.1


    【解决方案1】:

    假设一个基本模型:

    public class Resume
    {
    public int ResumeID {get;set;}
    public string Name { get; set; }
    public byte[] Resume { 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 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");
        }
    

    这是我在数据库中存储文件时实现的基本方法。

    【讨论】:

      【解决方案2】:

      查看文件

      查看

       @{
           if (Model.Logo != null)
             {
               string imageBase64 = Convert.ToBase64String(Model.Logo);
               string imageSrc = string.Format("data:image/gif;base64,{0}", imageBase64);
               <img src="@imageSrc" width="100" height="100" />
             }
        }
      

      【讨论】:

      • 您的代码似乎只在文件是图像时才解决,而不是在它只是 doc 文件时,我相信如果您保存一个已上传文件/文件的表单,那么您将如何处理查看该表格并查看所述文件,因此他的代码用于 .pdf 或 .doc
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多