Force pdf file download window from iframe

2019-06-09 11:16发布

问题:

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.

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}
    });

回答1:

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