Sencha ExtJS PDF 下载

Sencha ExtJS PDF Download

我在 Sencha ExtJS 中有一个相当复杂的前端和 Python/Django 后端。

我需要通过 Django 后端下载 PDFs,这需要身份验证。我尝试了 iFrame 和 window.open,但我无法通过这种方法传递 auth 令牌。

如何通过身份验证从后台完成pdf下载?

提前致谢,

雷托

您可以尝试使用Ext.Ajax请求。像这样:

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