从iframe强制pdf文件下载窗口 [英] Force pdf file download window from iframe

查看:603
本文介绍了从iframe强制pdf文件下载窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个组件,这将允许我通过POST请求下载pdf,最好不要更改网站,或打开一个新窗口。

I'm trying to write a component, which will allow me to download pdf throught a POST request, preferably without changing site, or opening a new window.

我已经设法通过一个新的窗口下载​​它,但这不是我期望到底。以下是强制提交到隐藏的iframe的代码,但不幸的是这不会触发文件下载(我猜,浏览器在iframe中处理pdf)。

I already managed to download it through a new window, but that's not what I expect in the end. Below is the code which forces submit into a hidden iframe, but unfortunately this doesn't trigger file download (I guess, browser handles pdf inside of the iframe).

'我想知道如何解决这个问题。

I'm wondering how to solve that.

    var iframe = Ext.get('pdfGeneratorIframe');
    if(!iframe){
        var body = Ext.getBody();

        // create a hidden frame
        iframe = body.createChild({
            tag: 'iframe',
            cls: 'x-hidden',
            id: 'pdfGeneratorIframe',
            name: 'pdfGeneratorIframe'
        });
    }

    var form = Ext.create('Ext.form.Panel', {
        standardSubmit: true,
        url: reportDetails.url,
        method: 'POST'
    });
    form.submit({
        //target: '_blank', // Avoids leaving the page.
        target: 'pdfGeneratorIframe',
        params: {htmlCode: fullHtmlToPrint,
            baseUrl: document.URL,
            filename: reportDetails.filename}
    });


推荐答案

它只应该依赖于哪些http标头返回服务器。 Content-Disposition:attachment应该触发浏览器下载。

It should only depend on which http headers are returned from the server. "Content-Disposition: attachment" should trigger the browser to download.

这篇关于从iframe强制pdf文件下载窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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