2012-03-28 37 views
3

请帮忙。 我显然不是专家,但使用本网站的建议,我觉得我真的很接近做以下在Tab或iframe中打开动态生成的PDF

能够在 一)新选项卡 B)的iframe打开一个动态生成的PDF

希望我只需要几行正确的语法,我就会在那里。

我动态生成使用iTextSharp的

控制器

public FileStreamResult GetPdf() 
    { 
    ... 
    return new FileStreamResult(Response.OutputStream, "application/pdf"){FileDownloadName = "download.pdf"}; 
    } 

VIEW

<input id="btnNewTab" type="button" value="New Tab" /> 
<input id="btnIframe" type="button" value="Iframe" /> 

<div id="pdfDiv"></div> 

<script type="text/javascript"> 
    $(function() { 

    $("#btnIframe").click(function() { 
     $.get('/PDFTest/GetPdf', function (pdf) { 
     alert(pdf.length); // Works as expected 
     // What do I need to put here to get the pdf to appear in a iframe 
     }); 
    }); 

    $("#btnNewTab").click(function() { 
     // asks me if I want to Open or Save the PDF. 
     // If I choose Open, the PDF opens in a completely new Window. 
     // Instead of the dialog, I just want the PDF to open in a new Tab 
     // I am probably going about this in completely the wrong way. 
     var HTML = "<iframe src='/PDFTest/GetPdf' style='width: 100%; height: 600px' ></iframe>"; 
     $('#pdfDiv').html(HTML); 
    }); 

    }); 
</script> 

针对您的建议达林控制器中的PDF,我改变了控制器到:

public FileStreamResult GetPdf(someInfo from View) 
    { 
    ... 
     Response.ContentType = "application/pdf"; 
     Response.AddHeader("Content-Disposition", "inline;test.pdf"); 
     Response.Buffer = true; 
     Response.Clear(); 
     Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length); 
     Response.OutputStream.Flush(); 
     Response.End(); 

     return new FileStreamResult(Response.OutputStream, "application/pdf"); 
    } 

完成之后,你的建议运行良好,但我意识到我没有足够清楚地解释我的意图。因此,我改变了VIEW以反映我正在尝试做的事情。

input id="btnNewTab" type="button" value="New Tab" /> 
<input id="btnIframe" type="button" value="Iframe" /> 
<iframe id="iframe"></iframe> 
<div id="pdfDiv"> 
</div> 
<script type="text/javascript"> 
    $(function() { 

    $("#btnIframe").click(function() { 

     $.ajax({ 
     url: '/PDFTest/GetPdf', 
     type: "GET", 
     data: json, // This line will not be a problem 
     dataType: "json", 
     contentType: "application/pdf", // This line might be a problem 
     success: function (pdf) { 
      // What to I need to need to do to display the PDF in the above iframe 
      $("#iframe").attr("src", pdf); // HTTP Error 400 - Bad Request. 
     } 
     }); 
    }); 

    $("#btnNewTab").click(function() { 
     $.ajax({ 
     url: '/PDFTest/GetPdf', 
     type: "GET", 
     data: json, // This line will not be a problem 
     dataType: "json", 
     contentType: "application/pdf", // This line might be a problem 
     success: function (pdf) { 
      // What to I need to need to do to disply the PDF in a new window 
     } 
     }); 
    }); 

    }); 
</script> 

回答

5

操作:

public ActionResult GetPdf() 
{ 
    byte[] pdf = ... 
    Response.AppendHeader("Content-Disposition", "inline;test.pdf"); 
    return File(pdf, "application/pdf"); 
} 

要在新标签/窗口打开:

@Html.ActionLink("view pdf", "getpdf", "somecontroller", null, new { target = "_blank" }) 

要在iframe代码看起来不错打开。只要确保将Content-Disposition标题设置为内联。

+0

非常感谢您的回复。 我真的很希望看到我对原始文章所做的修改之后,您会很乐意帮助完成这些修改。 – 2012-03-28 14:38:10

+0

@PeteDavies,在你的例子中你使用的是AJAX。您不能使用AJAX下载文件。 GetPdf操作不应该是使用AJAX请求的查询。 – 2012-03-28 14:43:33

+0

哦!在这种情况下,我如何向GetPdf传递它所需的信息? – 2012-03-28 14:47:51