简体   繁体   English

单击按钮,在新窗口或选项卡中打开流式页面

[英]on clicking a button, opening a streamed page in new window or tab

I am using .NET4 MVC3 VS2010.我正在使用 .NET4 MVC3 VS2010。 We are using ABCpdf to convert html to pdf and stream it to the browser.我们正在使用 ABCpdf 将 html 转换为 pdf 并将其流式传输到浏览器。 The problem is that I need to figure out a way for the pdf be opened in a new window or tab.问题是我需要找出一种在新窗口或选项卡中打开 pdf 的方法。 I have read a bunch of posts on similar issue, but could find nothing to help with this specific case.我已经阅读了很多关于类似问题的帖子,但在这个特定案例中找不到任何帮助。 Upon clicking a button (not a link) on a page, a controller action takes care of creating a memory stream from the html for that very page and then converts it to pdf and sends it to the browser.单击页面上的按钮(不是链接)后,控制器操作负责从该页面的 html 创建内存流,然后将其转换为 pdf 并将其发送到浏览器。

Code in the view for that page: var pdfUrl = String.Format("window.location.href = '{0}';", Html.BuildUrlFromExpressionForAreas(c => c.GeneratePdf(myUrl)));该页面的视图中的代码: var pdfUrl = String.Format("window.location.href = '{0}';", Html.BuildUrlFromExpressionForAreas(c => c.GeneratePdf(myUrl))); } @(Html.Button("btnPdf", "Generate PDF", HtmlButtonType.Button, pdfUrl))) } @(Html.Button("btnPdf", "生成PDF", HtmlButtonType.Button, pdfUrl)))

Code in MyController action: public ActionResult GeneratePdf(string url) Doc pdfDoc = new Doc(); MyController 操作中的代码: public ActionResult GeneratePdf(string url) Doc pdfDoc = new Doc(); int docId;内部文档ID; MemoryStream outputStream = new MemoryStream(); MemoryStream outputStream = new MemoryStream();

and a bunch of other code for pdf conversion..... then: byte[] pdfData = pdfDoc.GetData();和一堆其他的pdf转换代码.....然后: byte[] pdfData = pdfDoc.GetData(); outputStream.Write(pdfData, 0, pdfData.Length); outputStream.Write(pdfData, 0, pdfData.Length); outputStream.Position = 0; outputStream.Position = 0; return new FileStreamResult(outputStream, "application/pdf");返回新的 FileStreamResult(outputStream, "application/pdf"); } }

It works fine but the pdf is displayed in the same window/tab.它工作正常,但 pdf 显示在同一窗口/选项卡中。 How can I stream the data to a new window or tab in the browser?如何将数据流式传输到浏览器中的新窗口或选项卡? I am a newbee and would appreciate detailed help.我是新手,希望得到详细的帮助。 target property does not work for this case.目标属性不适用于这种情况。 I could not work window.open into the view code for the button and I am not sure that would do the trick either.我无法将 window.open 工作到按钮的视图代码中,我也不确定这是否可以解决问题。 Thanks in advance提前致谢

You could use a normal link with target="_blank" attribute:您可以使用带有target="_blank"属性的普通链接:

@Html.ActionLink(
    "Generate PDF", 
    "GeneratePdf", 
    "MyController", 
    null,  
    new { 
        url = "some url",
        target = "_blank"
    }
)

or if you want a button you could use javascript:或者如果你想要一个按钮,你可以使用 javascript:

<input type="button" value="Generate PDF" id="btnPdf" data-pdf-url="@Url.Action("GeneratePdf", "MyController", new { url = "some url" })" />

and then in a separate javascript file you could use jQuery and subscribe for the click event of this button and open a new window:然后在单独的 javascript 文件中,您可以使用 jQuery 并订阅此按钮的单击事件并打开一个新窗口:

$(function() {
    $('#btnPdf').click(function() {
        // get the url of the data-pdf-url property of the button
        var url = $(this).data('pdf-url');
        window.open(url, 'pdf');
        return false;
    });
});

@Darin Dimitrov , Thanks, the page was missing the reference. @Darin Dimitrov,谢谢,该页面缺少参考。 I fixed that problem, but it behaves strangely: The first time I click the button it doesn't do anything (doesn't go back to any action in the controller.) the second time I click the button it opens a new tab but gives an error that he resource is unavailable and it shows it is looking for a view called undefined!我解决了这个问题,但它的行为很奇怪:我第一次单击按钮时它什么也不做(不会返回控制器中的任何操作。)第二次单击按钮时它会打开一个新选项卡,但是给出一个错误,他的资源不可用,它表明它正在寻找一个名为 undefined 的视图! if I ignore this and go back to the original page and click the same button for the third time then it behaves as I hoped it would on the first click: it opens a new tab and displays the original page data streamed and formatted in pdf, all correctly!如果我忽略这一点并返回原始页面并第三次单击相同的按钮,那么它的行为就像我希望在第一次单击时一样:它会打开一个新选项卡并显示以 pdf 格式流式传输和格式化的原始页面数据,一切正确! any suggestions?有什么建议? I really appreciate your help with this.我真的很感谢你在这方面的帮助。 Here is my JS code:这是我的 JS 代码:

<script type="text/javascript">
 $(document).ready(function () {
     $("#btnPdf").click(function () {
         var url = $(this).attr("href");
         $(this).attr("href", '@MvcHtmlString.Create(Html.BuildUrlFromExpressionForAreas<MyController>(c => c.GeneratePdf(Request.Url.ToString())))').bind("click",
                function () {
                    var win = window.open(url);
                    win.focus();
                    return false;
                });
     });
   });
 </script> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM