我想在部分视图中显示我的PDF,其中下载选项不可用吗?

时间:2017-07-27 06:56:20

标签: asp.net-mvc-4

我想在没有下载选项的情况下显示PDF。经过多次搜索Google后,我得到了一些答案,但我遇到了一个问题。

PDF在部分视图中打开,但也有下载选项。是否有其他选项可以在没有下载选项的情况下打开Pdf?

 @model Bizzop.Models.MyAccountModel
    @{
       Layout = null;
     }

 <html>
  <head>
    <title>INDEX</title>
  </head>

  <body>

    <div id="divPartialView">

    </div>

      <div class="container">
       @if (Model.MyAccountList.Count > 0)
         {
           foreach (var items in Model.MyAccountList)
          {
           <div class="video-row">
            <a href="#" target="_blank" onclick="myPdf(this)"  
               id="@items.PdfName">
              <div class="row">
                @if (items.PdfName == "" || items.PdfName == null)
                {
                  <img src="ImageName"/>
                 }
                else
                 {
                    <img src="ImageName"/>
                }     
           </div>
          </a>
        </div>
           }
        }
    </div>

//////这是我们在锚点中点击用户时传递文件名的Ajax代码        标签

   <script>
    function myPdf(e) {
        debugger          
        var filen = e.id;
        $.ajax({
            url: "/MyAccount/MyPdfResult",
            data: { pdfname: filen },
            cache: false,
            type: "POST",
            dataType: "html",                
            type: "post",                
            success: function (data) {                   
               SetData(data);
            },
            error: function (data) {

            }  
        });
        function SetData(data) {

            $("#divPartialView").html(data); // HTML DOM replace
        }
    }
</script>

///////////在控制器中

     public ActionResult MyPdfResult(string pdfname = null)
    {
        string embed = "<object data=\"{0}\" type=\"application/pdf\" 
                        width=\"500px\" height=\"300px\">";          

        embed += "</object>";
       TempData["Embed"] = string.Format(embed,                    
       VirtualPathUtility.ToAbsolute("~/Content/TutorialImage/TutorialPdf/"+ 
       pdfname));            
         return PartialView("_Viewpdf", TempData["Embed"]);          
    }

/////我在哪里创建一个局部视图

    <div class="ancor">
       @Html.Raw(TempData["Embed"])
   </div>

1 个答案:

答案 0 :(得分:0)

我只是试着在我身边,它对我有用。您需要确保允许您的应用访问pdf文件。 这是我的代码:

控制器:

    [HttpPost]
    [AllowAnonymous]
    public ActionResult MyPdfResult(string pdfname = null)
    {
        string embed = "<object data=\"{0}\" type=\"application/pdf\" width=\"500px\" height=\"300px\">";
        embed += "If you are unable to view file, you can download from <a href = \"{0}\">here</a>";
        embed += " or download <a target = \"_blank\" href = \"http://get.adobe.com/reader/\">Adobe PDF Reader</a> to view the file.";
        embed += "</object>";
        TempData["Embed"] = string.Format(embed, VirtualPathUtility.ToAbsolute("~/Files/pdf.pdf"));

        return PartialView("_Viewpdf", TempData["Embed"]);
    }

部分视图:

     <style type="text/css">
body {
    font-family: Arial;
    font-size: 10pt;
}

@using (Html.BeginForm("MyPdfResult", "Home", FormMethod.Post))
{
    <a href="javascript:;" onclick="document.forms[0].submit();">View PDF</a>
    <hr />
    @Html.Raw(TempData["Embed"])
}

指数:

<div>
@Html.Partial("_Viewpdf");
</div>

Like this it worked for me: