Skip to Content
0
Nov 18, 2019 at 10:09 AM

SAPUI5 File Upload and POST json data.

1053 Views Last edit May 21, 2020 at 03:06 PM 2 rev

Hi, experts

I want to POST JSONdata to hanaDB from csv.File.

First, I wrote a code to import csv file and convert it to JSONdata.and then I try to post JSONdata to hanaDB.However,an error occurred. that is below.Log-dbg.js:414 2019-11-18 18:31:08.439485 The following problem occurred: HTTP request failed400,Bad Request, http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"> xml:lang="en-US">Error processing request stream. JSON text specified is not valid. - }
this is my source Code. Object.controller.js
sap.ui.define([
	"./BaseController",
	"sap/ui/model/json/JSONModel",
	"sap/ui/unified/FileUploader",
	"sap/m/Dialog"
], function (BaseController, JSONModel, FileUploader, Dialog) {
	"use strict";


	return BaseController.extend("myApp.Upload.controller.Object", {
		onInit: function () {
			var oViewModel = new JSONModel({
				busy: true,
				delay: 0,
				items: []
			});
		//	this.getRouter().getRoute("RouteObject").attachPatternMatched(this._onObjectMatched, this);
			// Store original busy indicator delay, so it can be restored later on
		//	iOriginalBusyDelay = this.getView().getBusyIndicatorDelay();
			this.setModel(oViewModel, "objectView");
		//	this.getOwnerComponent().getModel().metadataLoaded().then(function() {
				// Restore original busy indicator delay for the object view
		//		oViewModel.setProperty("/delay", iOriginalBusyDelay);
		//	});
		},
		onAddExcelData: function() {
			var that = this;
		
			if (this.fixedDialog === undefined) {
				this.fixedDialog = new Dialog({
					title: "Choose CSV File For Upload",
					beginButton: new sap.m.Button({
						text: "Upload",
						press: function(oEvent) {


							that.fixedDialog.close();
						}
					}),
					content: [
						new FileUploader("excelUploader")
					],
					endButton: new sap.m.Button({
						text: "Cancel",
						press: function() {
							that.fixedDialog.close();
						}
					})
				});


				this.getView().addDependent(this.fixedDialog);
				this.fixedDialog.attachBeforeClose(this.setDataToJsonFromExcel, this);
			}


		
			this.fixedDialog.open();
		},
		setDataToJsonFromExcel: function(oEvent) {
			var oUploader = oEvent.getSource().getContent()[0];
			var domRef = oUploader.getFocusDomRef();
			if (domRef.files.length === 0) {
				return;
			}
			var file = domRef.files[0];
			var that = this;
			this.fileName = file.name;
			this.fileType = file.type;
			var reader = new FileReader();
			reader.onload = function(e) {
			?debugger;
				var arrCSV = e.currentTarget.result.match(/[\w-()/ .]+(?=,?)/g);
				//var arrCSV = e.currentTarget.result.match(/[\w .]+(?=,?)|\d{4}-\d{1,2}-\d{1,2}(?=,?)/g);
				 //var array = e.currentTarget.result.split(/\n/);
				 //for(var i = 0; i < array.length - 1; i++){
				 //	var arrCSV =+ array[i].split(",");
				 //}
				var noOfCol = 8;
				var headerRow = arrCSV.splice(0, noOfCol);
				var data = [];
				while (arrCSV.length > 0) {
					var record = {};
					var excelData = arrCSV.splice(0, noOfCol);
					for (var i = 0; i < excelData.length; i++) {
						record[headerRow[i]] = excelData[i].trim();
					}
					data.push(record);
				}
				that.getView().getModel("objectView").setProperty("/items", data);
			};
		
			reader.readAsText(file);
		},
		onSave: function() {
			debugger;
			var data = this.getView().getModel("objectView").getProperty("/items");
			this.oModelData = new sap.ui.model.odata.v2.ODataModel("/ITEM/demo/demo.xsodata");
			this.oModelData.create(
				"/itemdata", data[2]
				);
		}
	});
});
Object.view.xml
<mvc:View controllerName="myApp.Upload.controller.Object"
xmlns="sap.m"
xmlns:mvc="sap.ui.core.mvc" 
xmlns:semantic="sap.m.semantic" 
xmlns:unf="sap.ui.unified" 
xmlns:footerbar="sap.ushell.ui.footerbar" 
displayBlock="true">
<Page id="page" title="{i18n>title}">
<Panel headerText="Excel Upload">
<Table
id="idItems"
width="auto"
class="sapUiResponsiveMargin"
growing="true"
items="{objectView>/items}"
growingScrollToLoad="false">
<headerToolbar>
<Toolbar>
<Title id="idTopTitle" text="{i18n>title}"/>
<ToolbarSpacer />
<Button icon="sap-icon://add" press="onAddExcelData"></Button>
<Button icon="sap-icon://save" press="onSave"></Button>
</Toolbar>
</headerToolbar>
<columns>
<Column id="idNumber">
<Text text="??"/>
</Column>
<Column id="idDate">
<Text text="????"/>
</Column>
<Column id="idPrice">
<Text text="??"/>
</Column>
<Column id="idItem">
<Text text="??"/>
</Column>
<Column id="idPurcahesr">
<Text text="???"/>
</Column>
<Column id="idComment">
<Text text="??"/>
</Column>
<Column id="idStatus">
<Text text="?????"/>
</Column>
<Column id="idApprover">
<Text text="???"/>
</Column>
</columns>
<items>
<ColumnListItem>
<cells>
<Text text="{objectView>NUMBER}"></Text>
<Text text="{objectView>DATE}"></Text>
<Text text="{objectView>PRICE}"></Text>
<Text text="{objectView>ITEM_TYPE}"></Text>
<Text text="{objectView>EMPLOYEE}"></Text>
<Text text="{objectView>COMMENT}"></Text>
<Text text="{objectView>STATUS}"></Text>
<Text text="{objectView>APPROVER}"></Text>
</cells>
</ColumnListItem>
</items>
</Table>
</Panel>
</Page>

</mvc:View>
And I want to convert format like this /Date(1573430400000)/ from 2019-11-11Something wrong?

Hope this helps.