retokiefer
retokiefer

Reputation: 109

Sencha ExtJS PDF Download

I have a rather complex frontend in Sencha ExtJS with a Python/Django Backend.

I need to download PDFs via the Django Backend, which needs authentication. I tried an iFrame and window.open but I can not pass the auth token with this approach.

How can I accomplish the pdf download from the backend with authentication?

Thanks in advance,

Reto

Upvotes: 0

Views: 253

Answers (1)

Arthur Rubens
Arthur Rubens

Reputation: 4706

You can try to use Ext.Ajax request. Something like this:

const authToken = 'SomeAuthToken';
const fileName = 'SomeFile.pdf';
const fileType = 'application/pdf';

Ext.Ajax.request({
    url: './' + fileName,
    headers: {
        'Authorization': authToken
    },
    binary: true,
    success: function (response, opts) {
        var windowUrl = window.URL || window.webkitURL;
        var url = windowUrl.createObjectURL(new Blob([response.responseBytes], {type: fileType}));
        var anchor = document.createElement('a');
        anchor.href = url;
        anchor.download = fileName;
        anchor.click();
    },

    failure: function (response, opts) {
        console.log('server-side failure with status code ' + response.status);
    }
});

Upvotes: 1

Related Questions