因此,我尝试下载docx文档作为报告。通过将ajax响应转换为blob,然后转换为url!结果是显示信息的文档单词,“对不起,我们无法打开,因为我们发现它的内容有问题。
那里主要:
onDownloadReport:函数(oEvent){

var oAjaxBody = {
    SessionId: this.getModel("sessionModel").getData().SessionId,
    CustomerName: encodeURIComponent(this.getModel("sessionModel").getData().CustomerName.split("\n")[0]),
    TenantInfo: encodeURIComponent(this.getModel("sessionModel").getData().TenantInfo)
        };
var sServiceUrl = "/SDC_XS_TEMP/APPL/SDC/services/serviceRuntime/xsjs/SDC_REPORT_GENERATE_MM.xsjs";

var me = this;
$.ajax({
    url: sServiceUrl,
    type: "POST",
    responseType:'arraybuffer',
    contentType: "application/json",
    data: JSON.stringify(oAjaxBody),
    // dataType: "json",
    success: function (oAjaxResponse) {
        var content = oAjaxResponse;
        var fileName = 'rapport.docx'; // You can use the .txt extension if you want
        me.downloadwithpost(fileName, content);
    },
    error: function (oError) {
        console.log("failure");
    }

});

这是功能
downloadwithpost: function (filename, content) {
    var link = document.createElement('a');
    var bytes = new Array(content.length);
    // var bytes = new Array(content.length);
    for (var i = 0; i < content.length; i++) {
        bytes[i] = content.charCodeAt(i);
    }
    var byteArray = new Uint8Array(bytes);
    var blob = new Blob([byteArray], {
        type: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
    });
    var url = URL.createObjectURL(blob);
    var oItem = {
        documentId: url,
        fileName: "rapport.docx",
        thumbnailUrl: "",
        url: url,
        selected: true
    };
    var oUploadCollection = this.getView().byId("uploadCollection");
    var newItem = new sap.m.UploadCollectionItem(oItem);
    oUploadCollection.addItem(newItem);
    oUploadCollection.downloadItem(newItem, true);
}

最佳答案

看一下这个示例,我使用的是十六进制数据格式,但是您可以根据需要进行更改。让我知道您是否需要有关此示例的帮助或有任何疑问。

   doAfterSuccess: function(result, fileName, fileType) {
            var filedata = result.ARRAY[0].FILEDATA; // get data from response
            // ------------------------------- hex data, rapport, docx
            var createdFile = this.createFile(filedata, fileName, fileType);
            this.downloadFile(createdFile, fileName, fileType);
    },

    createFile: function(hexContent, filename, type) {
        var data = this.convertHexToBinary(hexContent); // skip if your data is not hex
        var file = new Blob([data], {
            name: filename,
            type: type
        });
        return file;
    },

    convertHexToBinary: function(hexContent) {
        return new Uint8Array(hexContent.match(/.{2}/g).map(function(e)
        {
            return parseInt(e, 16);
        }));
    },
    downloadFile: function(file, filename, type){
        if (window.navigator.msSaveOrOpenBlob)
            window.navigator.msSaveOrOpenBlob(file, filename);
        else { // Others
            var a = document.createElement("a"),
                url = URL.createObjectURL(file);
            a.href = url;
            a.download = filename + "." + type;
            document.body.appendChild(a);
            a.click();
            setTimeout(function() {
                document.body.removeChild(a);
                window.URL.revokeObjectURL(url);
            }, 0);
        }
    },

09-18 15:03