javascript - SAPUI5 未捕获类型错误 : jszip is not a constructor

标签 javascript sapui5

我正在开发一个sapui5应用程序来上传.xlsx文件并读取上传文件的数据。我已按照以下网址的评论部分中的说明进行操作

https://archive.sap.com/discussions/thread/3782341

正如它提到的,我已将 xlsx.js 和 jszip.js 添加到项目(libs 文件夹)中 我已经使用上面 URL 中提到的以下代码更改了 jszip.js 文件的前几行代码

!function(e){ if(typeof exports==="object"&& typeof module!=="undefined"){module.exports=e();}

else if(typeof define==="function"&&define.amd){define([],e);}else{var f;typeof window!=="undefined"?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self

&&(f=self),f.JSZip=e();}}(function(){

return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require==="function"&&require;if(!u&&a){return a(o,!0);}

if(i){return i(o,!0);}

throw new Error("Cannot find module'"+o+"'");}var f=n[o]={exports:{}};

t[o][0].call(f.exports,function(e){var n=t[o][1][e];

return s(n?n:e);},f,f.exports,e,t,n,r);}return n[o].exports;}var i=typeof require==="function"&&require;for(var o=0;o<r.length;o++){s(r[o]);}

return s;})({1:[function(_dereq_,module,exports){

'use strict';

// private property

var _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

早期,当我在 Web IDE 中运行应用程序以及部署到 sap cloud launch pad 时,它运行良好。

但是今天,当我再次检查应用程序的部署版本时,它不起作用并给出以下错误。

xlsx.js?eval:11375 Uncaught TypeError: jszip is not a constructor at read_zip (xlsx.js?eval:11375) at Object.readSync [as read] (xlsx.js?eval:11396) at FileReader.reader.onload (Home.controller.js?eval:37)

xlsx.js文件的该函数出现错误

function read_zip(data, opts) {
    var zip, d = data;
    var o = opts||{};
    if(!o.type) o.type = (has_buf && Buffer.isBuffer(data)) ? "buffer" : "base64";
    switch(o.type) {
        case "base64": zip = new jszip(d, { base64:true }); break;
        case "binary": case "array": zip = new jszip(d, { base64:false }); break;
        case "buffer": zip = new jszip(d); break;
        case "file": zip=new jszip(d=_fs.readFileSync(data)); break;
        default: throw new Error("Unrecognized type " + o.type);
    }
    return parse_zip(zip, o);
}

但是当我使用 Web IDE 运行该应用程序时,该应用程序运行良好。

这就是我使用包含的 js 库的方式

jQuery.sap.require("SampleFileApp.libs.xlsx");
jQuery.sap.require("SampleFileApp.libs.jszip");

sap.ui.define([
    "sap/ui/core/mvc/Controller"
], function(Controller) {
    "use strict";

    return Controller.extend("SampleFileApp.controller.Home", {
            handleExcelUpload: function(e) {

            var fileUpload = this.getView().byId("idfileUploader");
            var domRef = fileUpload.getFocusDomRef();
            var file = domRef.files[0];
            this._import(file);

        },

        _import: function(file) {
            var that = this;
            if (file && window.FileReader) {

                //Initialize Reader
                var reader = new FileReader();
                var result = {},
                    data;

                reader.onload = function(e) {
                    data = e.target.result;
                    //get workbook data as binary
                    var wb = XLSX.read(data, {
                        type: 'binary'
                    });
                    wb.SheetNames.forEach(function(sheetName) {
                        var roa = XLSX.utils.sheet_to_row_object_array(wb.Sheets[sheetName]);
                        if (roa.length > 0) {
                            result[sheetName] = roa;
                        }

                    });
                    //Display Uploaded data
                    //that.displayUploadedData(result);
                    //that.displayData(result);
                    that.bindTable(result);
                    return result;

                };

                reader.readAsBinaryString(file);

            }

        }
};

而且我还在 component.js 文件中添加了 xlsx.js 的 js 引用

jQuery.sap.require("SampleFileApp.libs.xlsx");


sap.ui.define([
    "sap/ui/core/UIComponent",
    "sap/ui/Device",
    "SampleFileApp/model/models"
], function(UIComponent, Device, models) {
    "use strict";

    return UIComponent.extend("SampleFileApp.Component", {

        metadata: {
            manifest: "json"
        },

        /**
         * The component is initialized by UI5 automatically during the startup of the app and calls the init method once.
         * @public
         * @override
         */
        init: function() {
            // call the base component's init function
            UIComponent.prototype.init.apply(this, arguments);

            // set the device model
            this.setModel(models.createDeviceModel(), "device");
        }
    });
});

无法识别代码中的任何差异。仅当部署到云启动板时这才起作用

最佳答案

添加文件/dist/xlsx.full.min.js,而不是 xlsx.js。 https://github.com/SheetJS/sheetjs/blob/master/dist/xlsx.full.min.js

关于javascript - SAPUI5 未捕获类型错误 : jszip is not a constructor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51989522/

相关文章:

javascript - 除了使用 Date.parse() 之外的选项

javascript - 将数据推送到 JSONModel

javascript - UI5 将新数据从 Controller 绑定(bind)到模型

javascript - 从 React Hook useEffect 内部对 'timeInterval' 变量的赋值将在每次渲染后丢失

javascript - 开放层 3 : Zoom map to coordinates in array

javascript - knockout ,CSS 动态和条件绑定(bind)

javascript - 导入Json数据无法绑定(bind)查看

javascript - 在 .NET 业务应用程序上使用 SAP OpenUI5

javascript - 如何从 JSON 捕获数据并用 HTML 显示

javascript - 在我看来,使用 iFrame 中使用的 html 文件中的模型数据