简体   繁体   English

通过js发送数据并从动作控制器接收pdf

[英]sending data over js and receive pdf from action controller

I'm sending two strings from my view to the controller using javascript. 我正在使用JavaScript从视图向控制器发送两个字符串。 Controller receive that strings and based on their values return pdf page. 控制器接收到该字符串,并根据其值返回pdf页面。 I'm using this approach for creating pdf on the fly. 我正在使用这种方法动态创建pdf。 Everything is fine, I'm getting pdf when I look inside firebug, but I don't know what to do inside javascript success operation, how to display this pdf on the view. 一切都很好,当我在萤火虫内部查看时,我得到的是pdf,但是我不知道在javascript成功操作中该怎么做,如何在视图上显示此pdf。

my js is 我的js是

function GetDetails2(xx, xxx) {
$.ajax({
         url: ('/PdfReport/MyreportDemo'),
         type: 'POST',
         contentType: 'application/json',
         data: JSON.stringify({ dataOne: xx, dataTwo: xxx }),
         success: function (result) {                 
                    //alert("ok");
                },    
         error: function () {
               alert("error");
        }
       });
 }

JavaScript isn't very good at handling binary data. JavaScript在处理二进制数据方面不是很好。 The only way to directly handle this is by encoding the data as a base-64 encoded data-uri . 直接处理此问题的唯一方法是将数据编码为base-64编码的data-uri Posting JSON trough an <iframe> won't work either, since you need a <form> . 通过<iframe>将JSON发布也将不起作用,因为您需要<form>

A possible solution would be to change the back-end to accept uri-parameters: 一个可能的解决方案是更改后端以接受uri参数:

/PdfReport/MyreportDemo?dataOne=xx&dataTwo=xx

Then you could use an <iframe> : 然后,您可以使用<iframe>

<iframe name="hiddenframe" style="display: none;" src="about:blank"></iframe>
<form id="reportDemoForm" method="post" action="/PdfReport/MyreportDemo" target="hiddenframe">
  <input id="dataOne" name="dataOne" type="hidden" value=""/>
  <input id="dataTwo" name="dataTwo" type="hidden" value=""/>
</form>
$('#dataOne').val('xx');
$('#dataTwo').val('xx');
$('#reportDemoForm').submit();

This assumes you want to download the file. 假设您要下载文件。 If you want do display it on the page, you can instead show the <iframe> , and size it appropriately. 如果要在页面上显示它,则可以显示<iframe> ,并适当调整其大小。

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

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