Open a new window on response with pdf attachment
jqGrid('navButtonAdd',"#pager2",{caption:"Save All",title:"Save & Create Receipt",onClickButton:function () {
var s;
s = jQuery("#list2").jqGrid('getDataIDs');
alert("selected");
$.ajax({
type: 'POST',
url:'http://localhost:3000/order/receipt',
data: {ids: s},
});
}});
With above code, I'm able to submit data to server and the on server side it will generate a pdf as attachment, now I 开发者_StackOverflowwant to view the pdf on response via a new window/tab on browser.
Anyway to do it?
thanks, lupind
If you return a URL to the just generated PDF then you can call window.open with that URL. But I wouldn't use an Ajax call if you want to open a new window to display the PDF anyway. In this case I would either:
- Use Javascript's window.open passing through the data in the URL to the page that generates the PDF and let it output to the browser's window (but it might be too much data in the URL)
- Create a special form to hold the data from your page and submit that form. This special form would have a target attribute set (target="_blank") to open it in a new window/tab.
For me, using two HTTP calls for one result (the generation and viewing of a PDF) is a waste.
You can use window.open()
to open a new window with a specified URL. Be careful, though, only to do this when necessary.
Excerpt from this link.
Use an iframe
with visibility:hidden
, then change the iframe
location to a downloadable file in javascript on the success callback.
var iFrame = document.getElementById('hiddenIFrame');
iFrame.src = theurlThatWillProduceTheFile;
精彩评论