-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathRead_Upload_Local_File
More file actions
114 lines (104 loc) · 3.99 KB
/
Read_Upload_Local_File
File metadata and controls
114 lines (104 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//XML View
<html:input id="myInput" style="display:none" type="file"/>
<Button icon="sap-icon://upload" press="uploadFile" text="Upload"/>
<Button icon="sap-icon://download" press="downloadTemplate" text="Template"/>
//Controller
onAfterRendering: function() {
var that = this;
function CSVToArray(strData, strDelimiter) {
// Check to see if the delimiter is defined. If not,
// then default to comma.
strDelimiter = (strDelimiter || ",");
// Create a regular expression to parse the CSV values.
var objPattern = new RegExp((
// Delimiters.
"(\\" + strDelimiter + "|\\r?\\n|\\r|^)" +
// Quoted fields.
"(?:\"([^\"]*(?:\"\"[^\"]*)*)\"|" +
// Standard fields.
"([^\"\\" + strDelimiter + "\\r\\n]*))"), "gi");
// Create an array to hold our data. Give the array
// a default empty first row.
var arrData = [
[]
];
// Create an array to hold our individual pattern
// matching groups.
var arrMatches = null;
// Keep looping over the regular expression matches
// until we can no longer find a match.
while (arrMatches = objPattern.exec(strData)) {
// Get the delimiter that was found.
var strMatchedDelimiter = arrMatches[1];
// Check to see if the given delimiter has a length
// (is not the start of string) and if it matches
// field delimiter. If id does not, then we know
// that this delimiter is a row delimiter.
if (strMatchedDelimiter.length && (strMatchedDelimiter != strDelimiter)) {
// Since we have reached a new row of data,
// add an empty row to our data array.
arrData.push([]);
}
// Now that we have our delimiter out of the way,
// let's check to see which kind of value we
// captured (quoted or unquoted).
if (arrMatches[2]) {
// We found a quoted value. When we capture
// this value, unescape any double quotes.
var strMatchedValue = arrMatches[2].replace(
new RegExp("\"\"", "g"), "\"");
} else {
// We found a non-quoted value.
var strMatchedValue = arrMatches[3];
}
// Now that we have our value string, let's add
// it to the data array.
arrData[arrData.length - 1].push(strMatchedValue);
}
// Return the parsed data.
return (arrData);
}
function CSV2JSON(csv) {
var array = CSVToArray(csv);
var objArray = [];
for (var i = 1; i < array.length; i++) {
objArray[i - 1] = {};
for (var k = 0; k < array[0].length && k < array[i].length; k++) {
var key = array[0][k];
objArray[i - 1][key] = array[i][k];
}
}
var json = JSON.stringify(objArray);
var str = json.replace(/},/g, "},\r\n");
return str;
}
function resetFormElement(e) {
//reset the elemetn
e.wrap("<form>").closest("form").get(0).reset();
e.unwrap();
}
var fileInput = $("#" + this.getView().byId("myInput").getId());
fileInput.change(
function(event) {
var fileReader = new FileReader();
fileReader.onload = function(evt) {
var content = evt.target.result;
var jsonContent = CSV2JSON(content);
//Read current model values
var currentData = that.oApplicationFacade.getApplicationModel("soc_cart").getData();
Array.prototype.push.apply(currentData.oShoppingCartItems, JSON.parse(jsonContent));
that.oApplicationFacade.getApplicationModel("soc_cart").setData(currentData);
//reset fileInput so that same file can be uploaded again
resetFormElement(fileInput);
};
fileReader.readAsText(event.target.files[0]); //Only one file at a time. Once file is read, "onload" of the file reader is triggered.
});
},
uploadFile: function() {
//input html element's(type file) 'click' is triggered, which will show file selection dialog.
//Once the file is selected, input html element's 'change' is triggered.
$("#" + this.getView().byId("myInput").getId()).click();
},
downloadTemplate: function() { //You can store the template file within the app
window.location = "/sap/bc/ui5_ui5/sap/zso_create_ext/webapp/util/UploadTemplate.csv";
}