<ui:FileUploader id="fileUploaderFS" multiple="false" />
<Button text="Upload" type="Accept" press="onUploadFile"/>
//This code is used for uploading documents and images
onUploadFile:function(){
var oFileUpload =
this.getView().byId("fileUploaderFS");
var domRef = oFileUpload.getFocusDomRef();
var file = domRef.files[0];
var that = this;
//This code is used for uploading image or document file
this.fileName =
this.getView().byId("FileName").getValue();
this.fileType = file.type;
var reader = new FileReader();
reader.onload = function (e) {
var vContent = e.currentTarget.result
that.updateFile(that.fileName, that.fileType, vContent);
}
reader.readAsDataURL(file);
}
//This code is used for uploading xlsx sheets
var oFileUpload =
this.getView().byId("fileUploaderTS");
var domRef = oFileUpload.getFocusDomRef();
var file = domRef.files[0];
var that = this;
this.fileName = this.getView().byId("TSFileName")
.getValue()+".csv";
this.fileType = file.type;
this.json_object=null
var reader = new FileReader();
reader.onload = function(e) {
var data = e.target.result;
var workbook = XLSX.read(data, {
type: 'binary'
});
console.log(workbook)
workbook.SheetNames.forEach(function(sheetName) {
var XL_row_object =
XLSX.utils.sheet_to_row_object_array(
workbook.Sheets[sheetName]);
if(XL_row_object.length!==0){
that.json_object = JSON.stringify(XL_row_object);
}
})
that.updateFile(that.fileName, that.fileType,
that.json_object);
};
reader.onerror = function(ex) {
console.log(ex);
};
reader.readAsBinaryString(file);
}
updateFile:function(fileName, fileType, vContent){
debugger
var payLoad={
Filename:fileName,
Filetype:fileType,
Filecontent:vContent
}
var that = this;
var serviceurl="/sap/opu/odata/sap/ZAPP_EMP1_SRV/";
var oModel =
new sap.ui.model.odata.ODataModel(serviceurl); oModel .update("/FILESet('"+payLoad.Filename+"')/$value",
payLoad,{
method: "PUT",
success: function(data) {
sap.m.MessageToast.show("FILE UPDATED SUCCESSFULLY");
},
error: function(e) {
alert("error");
}
})
},
getFile:function(fileName){
fileName= fileName.toLowerCase()
var that = this;
var serviceurl="/sap/opu/odata/sap/servicename_SRV/";
var oModel = new sap.ui.model.odata.ODataModel(serviceurl);
oModel .read("/EntitySet('"+fileName+"')/$value",{
method: "GET",
success: function(data,response) {
debugger;
var fName=data.Filename
var fType= data.Filetype;
var fContent =data.Filecontent;
// If the file is document IS pdf/msword/plain text
if(fType==="text/plain" || fType==="application/pdf" || fType==="application/msword" ){
fContent =atob(fContent);
File.save(fContent, fName, "txt", fType);
}
//If the file is excel sheet
else if(fName.includes(".csv")){
var JSONData=fContent;
Var ReportTitle =fName;
If JSONData is not an object then JSON.parse will parse the JSON string in an Object
var arrData = typeof JSONData !=
'object' ? JSON.parse(JSONData) : JSONData;
var CSV = '';
Set Report title in first row or line
CSV += ReportTitle + '\r\n\n';
//1st loop is to extract each row
for (var i = 0; i < arrData.length; i++) {
var row = "";
//2nd loop will extract each column and convert it in
string comma-seprated
for (var index in arrData[i]) {
row += '"' + arrData[i][index] + '",';
}
row.slice(0, row.length - 1);
//add a line break after each row
CSV += row + '\r\n';
}
if (CSV == '') {
alert("Invalid data");
return;
}
//Generate a file name
var fileName = "MyNewReport_";
//this will remove the blank-spaces from the title and replace it with an underscore
fileName += ReportTitle.replace(/ /g,"_");
//Initialize file format you want csv or xls
var uri = 'data:text/csv;charset=utf-8,' + escape(CSV);
// Now the little tricky part.
// you can use either>> window.open(uri);
// but this will not work in some browsers
// or you will not get the correct file extension
//this trick will generate a temp <a /> tag
var link = document.createElement("a");
link.href = uri;
//set the visibility hidden so it will not effect on your web-layout
link.style = "visibility:hidden";
link.download = fileName + ".csv";
//this part will append the anchor tag and remove it after automatic click
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
//If the file is image
else{
fContent =atob(data.Filecontent);
var byteNumbers= new Array(fContent.length);
for (let index = 0; index < fContent.length; index++) {
byteNumbers[index]=fContent.charCodeAt(index)
}
var byteArray= new Uint8Array(byteNumbers)
var blob= new Blob([byteArray],{type:fType});
var url=URL.createObjectURL(blob)
window.open(url,"_blank");
}
MessageToast.show("FILE Downloaded Succesfully");
},
error: function(e) {
console.log(e)
alert("error");
}
})
},
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
7 | |
6 | |
5 | |
4 | |
4 | |
4 | |
4 | |
4 | |
4 | |
3 |