I'm developing a sapui5 application to upload .xlsx file and read data of the uploaded file. And I've followed the instructions in the comment section of the following url
https://archive.sap.com/discussions/thread/3782341
And as it mentioned I've added xlsx.js & jszip.js in to the project (libs folder) and I've changed jszip.js files first few lines of code with following code as mentioned in the above URL
!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+/=";
Early it worked well when I run the application in web IDE & also when deployed to the sap cloud launch pad.
But today when I check the deployed version of the application again it is not working and gives the following error.
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)
The error occured in this function of the xlsx.js file
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);
}
But the application is working perfectly when I run it using web IDE.
This is how I use the included js libraries
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);
}
}
};
And also I've added js reference for the xlsx.js in the component.js file
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");
}
});
});
Cannot identify any deferences in the code. This is not working only when deployed to the cloud launch pad