使用 AJAX 调用显示 PDF [英] Display PDF using an AJAX call

查看:15
本文介绍了使用 AJAX 调用显示 PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过 AJAX 调用显示 PDF(在服务器端创建并作为 Web 流传递到客户端).我的代码如下:

I'm trying to display a PDF(which is created in the server side and pass to the client side as a web stream) through an AJAX call. My code is given below:

jQuery.ajax({
    type: "POST",
    processData: false,
    url: "aaaa.p?name=pdf",
    data: inputxml,
    contentType: "application/xml; charset=utf-8",
    success: function(data)
    {
      // here the data is the PDF stream i'm getting from the server side. 

    }
});

'inputxml' 包含服务器创建 PDF 的输入参数.以及包含 PDF 流的成功函数中的数据".有什么方法可以在 AJAX 调用的成功函数内打开浏览器上的 PDF 文件,而无需进行任何页面提交?在服务器端,PDF 也不是物理生成的.非常感谢您的帮助....

The 'inputxml' is containing input parameters for the server to create the PDF. and the 'data' in the success function containing PDF stream. Is there any way to open the PDF file on the browser inside the success function of the AJAX call with-out doing any page submit? In the server side the PDF is not physically generated also. Highly appreciate your help....

推荐答案

为什么要通过 AJAX 加载?为什么不在需要时生成的 IFRAME 中加载它.标准浏览器插件将在该 Iframe 中显示它.

Why do you load it via AJAX? Why don't you load it in an IFRAME that you generate when you need it. The standard browsers plugin will display it then inside that Iframe.

$('#link').click(function(e) {
    e.preventDefault(); // if you have a URL in the link
    jQuery.ajax({
        type: "POST",
        processData: false,
        url: "aaaa.p?name=pdf",
        data: inputxml,
        contentType: "application/xml; charset=utf-8",
        success: function(data)
        {
            var iframe = $('<iframe>');
            iframe.attr('src','/pdf/yourpdf.pdf?options=first&second=here');
            $('#targetDiv').append(iframe);
        }
    });
});

这篇关于使用 AJAX 调用显示 PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆