Kendo服务器导出POST请求标头

时间:2016-12-31 09:01:33

标签: javascript spring-mvc kendo-ui

我正在使用Kendo的服务器端导出功能,以便能够从服务器导出xlsx / pdf。这是我的配置

kendo.saveAs({
  dataURI: workbook.toDataURL(),
  fileName: "STRResult.xlsx",
  proxyURL:'/my/kendo/export/kendoServerExport',
  forceProxy:true,
  proxyTarget: "_blank"
});

这里的问题是我在Spring MCV中有一个服务器(没有代码访问权限),它有一个全局过滤器来检查某些标头。我希望能够在此帖子请求中设置所需的标题。

我需要使用哪种配置才能使其正常工作?

TIA

1 个答案:

答案 0 :(得分:-1)

我在Kendo UI论坛上为此提供了解决方案。 你可以找到它HERE

论坛上描述的解决方案:

我根据互联网上其他地方的一些信息为此构建了一个解决方案。

我想与您分享,因此Telerik可能希望在以后的某个版本中使用它;)

<a href="https://en.wikipedia.org/wiki/List_of_fallacies#Red_herring_fallacies">Red herring logic fallacies list</a>

这是我们的基础库(Insad)的一部分。

我想你明白了。如果您遗失了某些内容,我很乐意为您提供:)

而不是像以下那样调用'旧'的保存:

//#region -- Download file and save data to a local file --
    // Invoke call to webservice and save file
    // url - Url to get the file from
    // method - GET/POST method
    // defaultFilename - Default filename if none was given from the server
    , saveAs: function (url, method, defaultFilename)
    {
        var caller = this;

        Insad.ajaxMaskUI({
            url: url,
            type: method,
            headers: {
                'Authorization': Insad.GetBaseAuthenticationToken()
                // ... Put your headers in here ...
            },
            maskUI: Insad.defaultMaskUI
            , maskPageMsg: 'Downloading file'
        })
        .fail(function (xhr, textStatus, errorThrown) {
            //console.log('error:', textStatus, errorThrown, xhr.getAllResponseHeaders(), xhr);
            var msg = xhr.responseText;
            if (msg === undefined) {
                msg = 'Unknown error';
            }
            console.log('FileDownload failed -> Error handler. Errormessage: ', msg);
            ShowWarning(msg);

            caller.handleUIErrorDownload();
        })
        .done(function (data, textStatus, xhr) {
            caller.handleUISuccessDownload(xhr, defaultFilename);
        });
    }
    , handleUIErrorDownload: function ()
    {
        console.log('handleUIError');
        // TODO: Reset stuff / give user more info?
        // ... Put your code in here ...
    }
    , handleUISuccessDownload: function (xhr, defaultFilename)
    {
        console.log('handleUISuccess');

        // Save file
        var filename = Insad.GetFilenameFromContent(xhr);
        if (filename === '') {
            filename = defaultFilename;
        }
        Insad.SaveDataToFile(filename, xhr.responseText, xhr.getResponseHeader('Content-Type'));

        // TODO: Give user more info?
        // ... Put your code in here ...
    }
//#endregion

//#region -- Save data to a local file --
    // Get the filename from the Content-Disposition in the response xhr
    , GetFilenameFromContent: function (xhr)
    {
        var filename = '';
        var disposition = xhr.getResponseHeader('Content-Disposition');
        // Only when inline or attachment are supported at this moment
        if (disposition && (disposition.indexOf('attachment') !== -1 || disposition.indexOf('inline') !== -1)) {
            var filenameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/;
            var matches = filenameRegex.exec(disposition);
            if (matches != null && matches[1]) {
                filename = matches[1].replace(/['"]/g, '');
            }
        }
        return filename;
    }

    // The function that will write the data to a file :)
    , SaveDataToFile: function (filename, data, mimeType)
    {
        var dataFileAsBlob = new Blob([data], {type:mimeType});
        var downloadLink = document.createElement("a");
        downloadLink.download = filename;
        downloadLink.innerHTML = "Download";
        if (window.webkitURL !== null) {                        // Is it Chrome based?
            // Chrome allows the link to be clicked
            // without actually adding it to the DOM.
            downloadLink.href = window.webkitURL.createObjectURL(dataFileAsBlob);
        }
        else {
            // Firefox requires the link to be added to the DOM
            // before it can be clicked.
            downloadLink.href = window.URL.createObjectURL(dataFileAsBlob);
            downloadLink.onclick = destroyClickedElement;       // --> TODO: ?? TEST ?? On Opera use: downloadLink.onclick = document.body.removeChild(event.target);
            downloadLink.style.display = "none";
            document.body.appendChild(downloadLink);
        }
        // Force download
        downloadLink.click();
    }
//#endregion

您现在可以使用:

kendo.saveAs({
    dataURI: url,
    fileName: defaultFilename,
    forceProxy: false
});

我遗漏了动画部分,但我认为这不应该很难:)