【问题标题】:How to show a preview of an uploaded document in ASP.Net MVC 5如何在 ASP.Net MVC 5 中显示上传文档的预览
【发布时间】:2018-02-04 04:50:20
【问题描述】:

我有一个表单,我从用户那里获取基本的简历信息,然后有一个上传按钮用于上传简历。现在我想要一个“详细信息”页面,我可以在其中显示简历表单的用户输入的详细信息。我能够显示除(上传的文件)之外的所有模型属性。有什么方法可以显示在详细信息页面中上传的文档的某种预览窗格。

视图模型:

public class ResumeViewModel
{
    [Required(ErrorMessage = "Enter Resume Name.")]
    [Display(Name = "Resume Name")]
    public string ResumeName { get; set; }

    [FileType("pdf|doc|docx|PDF", ErrorMessage = "File type is not valid.")]
    [Required]
    [Display(Name = "Upload Resume")]
    public HttpPostedFileBase UploadedResume { get; set; }
}

目前,我可以使用以下代码下载文档

   public FileContentResult Download(int? resumeId)
        {
            var temp = _context.Resumes.Where(f => f.ResumeId == resumeId).SingleOrDefault();
            var fileRes = new FileContentResult(temp.Content.ToArray(), temp.ContentType);
            fileRes.FileDownloadName = temp.FileName;
            return fileRes;
        }

但是如何在某些页面中显示它以及其他模型属性?

【问题讨论】:

  • 参考this question 了解一些选项
  • 看起来不像是直截了当的事情。 :-|
  • @StephenMuecke:我不能使用 I 框架或其他东西来显示小预览窗口等等。只是想知道。
  • 我不希望整个网页都显示文档。只是网页中的小可滚动区域。
  • 可能是一种选择,但您为什么要这样做? (无论如何,您已经有效地将文档发送给客户)

标签: c# asp.net asp.net-mvc razor asp.net-mvc-5


【解决方案1】:

你可以用对象标签来做到这一点

首先创建一个页面来查看上传的文件

cshtml

<object data='@Url.Action("GetFile")' type="@Viewbag.Type" width="700" height="700"></object>

在文件控制器中

     public ActionResult Index(int? resumeId)
                {               

                  var temp = _context.Resumes.Where(f => f.ResumeId == 
                  resumeId).SingleOrDefault();
                  var fileRes = new FileContentResult(temp.Content.ToArray(), 
                  temp.ContentType);
                  Session["Path"]= temp.FileName;
                  Session["Type"]=temp.ContentType
                  return View();
                }
and Action of File

public ActionResult GetFile()
        {
            if (Session["Path"] != null)
            {
                FileStream fileStream = new          
                FileStream(Session["Path"].ToString(), FileMode.Open, 
                FileAccess.Read);
                ViewBag.Type=Session["Type"]
                return File(fileStream, Session["Type"]);
            }
            return View("NotFound");
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-14
    • 2018-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多