cancel
Showing results for 
Search instead for 
Did you mean: 

How to upload the Excel file into the backend by using Odata service?

Hello Colleagues,

Is there any way to upload the Excel file from frontend and send it to the backend by using Odata service? I was trying to store that Excel file as a blob type into the backend. There are two sheets in the Excel file. Therefore, I created two JSON objects to store each sheet and combined those two JSON Objects into one JSON object which was "json1".

The excel file data was stored into the blob successfully and the size of the blob is 12650. When I console out the "json1" and all the excel data could print it out.

Because the blob is about a list of binary data. ( var blob = new Blob([arraybuffer]) Therefore, I was trying to convert the JSON object into the base64 string on the line 366. And then I converted the base64 string into the arraybuffer as the blob part.

The error message was "Request contains properties that do not exist in entity".

Is this the correct way to upload the Excel file as a blob type into the backend by using Odata service? Or Can any one provide the reference links on how to upload the Excel file into the backend by using OData service?

The backend table:

The oData service

Thank you!

Wanzhen

0 Kudos
onUpload:function(){
                var oFileUploader=this.getView().byId("fileUploader");
                 var domRef=oFileUploader.getFocusDomRef();
                 var file=domRef.files[0];
                 var that=this;
                var reader=new FileReader();
                reader.onload=function(e){
                //convert the excel file into arraybuffer
                    var data= e.target.result;
                    var workbook= XLSX.read(data,{
                        type:'array'
                    });
                var sheetOneName=workbook.SheetNames[0];
                var sheetOne=workbook.Sheets[sheetOneName];
                 var sheetTwoName=workbook.SheetNames[1];
             var sheetTwo=workbook.Sheets[sheetTwoName];
                
                var json1=XLSX.utils.sheet_to_json(sheetOne);
                var json2=XLSX.utils.sheet_to_json(sheetTwo);
                json1=json1.concat(json2);
                
            //convert the json object to base64 string
            var enc = window.btoa(unescape(encodeURIComponent(JSON.stringify(json1))));
            
            //convert base64 string to array buffer
             function base64ToArrayBuffer(base64){
             var binary_string=window.atob(enc);
             var len=binary_string.length;
             var bytes=new Uint8Array(len);
             for(var i=0;i<len;i++){
                 bytes[i]=binary_string.charCodeAt(i);
             }
             return bytes.buffer;
            }
                var blob = new Blob([base64ToArrayBuffer(enc)]);
                that.getView().getModel().setProperty("/TABLE_STATS_FI_DATA_MODEL",blob);
                },
                reader.readAsArrayBuffer(file);
            },


Accepted Solutions (0)

Answers (0)