C# MVC: Chrome using the action name to set inline PDF title

前端 未结 4 647
时光说笑
时光说笑 2021-01-11 10:42

I have an action who displays a PDF in a new browser tab.

    public ActionResult Print()
    {
        var cd = new ContentDisposition
        {
                    


        
4条回答
  •  长发绾君心
    2021-01-11 11:10

    I solved this problem by using a iframe.

    Create an action which fills the title and pdf url.

            public ActionResult ArticleSpecification(int ArticleID)
            {
                using (var context = new myEntities())
                {
                    Article article = context.Article.FirstOrDefault(a => a.ArticleID == ArticleID);
                    ViewData["Title"] = article.Code + " - " + article.Description;
                    ViewData["PdfSource"] = "ArticleSpecificationPdf?ArticleID=" + article.ArticleID;
                    return View("~/Views/Specification/pdfview.cshtml");
                }
            }
    

    pdfview.cshtml: with a iframe to view the pdf and title.

    @{
        Layout = "";
    }
    
    
        
            @ViewData["Title"]
            
        
        
            
        
    
    

    The action to return the pdf content.

            public FileResult ArticleSpecificationPdf(int ArticleID)
            {
                using (var context = new myEntities())
                {
                    PdfFile file = null;
                    Article article = context.Article.FirstOrDefault(a => a.ArticleID == ArticleID);
                    if (article != null && article.SpecificationPdfID != null)
                        file = context.PdfFile.FirstOrDefault(a => a.PdfFileID == article.SpecificationPdfID);
                    return new FilePathResult(file.path, "application/pdf");
                }
            }
    

提交回复
热议问题