我正在开发一个sapui5应用程序,以上传.xlsx文件并读取上传文件的数据。而且我已按照以下网址的注释部分中的说明进行操作
https://archive.sap.com/discussions/thread/3782341
并且正如我提到的,我已经将xlsx.js和jszip.js添加到项目(libs文件夹)中 并且我已经更改了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启动板中时,它都能很好地运行。
但是今天,当我再次检查应用程序的已部署版本时,它无法正常工作并出现以下错误。
xlsx.js?eval:11375未捕获的TypeError:jszip不是构造函数 在read_zip(xlsx.js?eval:11375) 在Object.readSync [读取时](xlsx.js?eval:11396) 在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");
}
});
});
无法识别代码中的任何引用。仅当部署到云启动板时,这才起作用
答案 0 :(得分:0)
添加文件/dist/xlsx.full.min.js,而不是xlsx.js。 https://github.com/SheetJS/sheetjs/blob/master/dist/xlsx.full.min.js
答案 1 :(得分:-1)
文件初始化从组件控制器开始,而不是index.html, 因此,在组件控制器中声明模块路径: 像这样:
jQuery.sap.require("com.sap.sbi.jszip");
jQuery.sap.require("com.sap.sbi.xlsx");
在组件控制器中。