Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
arun_krishna567
Explorer

Below is the approach that I tried and successfully able to upload File to back-end ECC server and upload data to database.


Frontend (UI part)


In XML view

<file:FileUploader id="fileupload" name="File Uploader"

              useMultipart="false" sendXHR="true" />

<Button id="btnUpload" press="uploadFile" text="Upload"></Button>


In Controller.js

uploadFile : function(){

var file = jQuery.sap.domById("__xmlview2--fileupload-fu").files[0];

                try {

                  if (file) {

                    this._bUploading = true;

                    var that = this;

/****************To Fetch CSRF Token*******************/

                      var a = "/Yourservice URL or Metadata URL ";

                      var f = {

                        headers : {

                          "X-Requested-With" : "XMLHttpRequest",

                          "Content-Type" : "application/atom+xml",

                          DataServiceVersion : "2.0",

                          "X-CSRF-Token" : "Fetch"

                        },

                        requestUri : a,

                        method : "GET"

                        };

                      var oHeaders;

                      var sUrl=oDataModel.sServiceUrl+"/Your Entity Set ";

                      var oModel = new sap.ui.model.odata.ODataModel(sUrl, true);                     

                      sap.ui.getCore().setModel(oModel);

                        OData.request(f, function(data, oSuccess) {

                          oToken = oSuccess.headers['x-csrf-token'];

                               oHeaders = {

                                            "x-csrf-token" : oToken,

                                            "slug" : "QF",

                                     };

/****************To Fetch CSRF Token*******************/

/*******************To Upload File************************/

            

                   var oURL = oDataModel.sServiceUrl+"/Entity Set";

                  jQuery.ajax({

                    type: 'POST',

                    url: oURL,

                    headers: oHeaders,

                    cache: false,

                    contentType: file.type,

                    processData: false,

                    data: file,

                    success: function(data){                      

                       var rec=data.getElementsByTagName("entry")[0].children[5].getAttribute("src");

                       sap.m.MessageToast.show("File Uploaded Successfully"+rec);

                    },

                    error:function(data){

                      sap.m.MessageToast.show("File Uploaded Successfully");

                    }

                  });

                  });

                }

                } catch(oException) {

              jQuery.sap.log.error("File upload failed:\n" + oException.message);

                }

}

/*******************To Upload File************************/

Note

For this blog the uploaded file excel file should be saved as .csv comma delimited format. If you upload (.XLSX, .XLS) format this code will not work for you.

In Backend ABAP System

Entity

Your entity should have this fields as mentioned above.


Implement Create_Stream Method


You need to implement /IWBEP/IF_MGW_APPL_SRV_RUNTIME~CREATE_STREAM method of DPC_EXT class


DATA lv_output TYPE string,
lv_out_tab
TYPE TABLE OF string,
ls_output
TYPE string.


DATA: gv_spl TYPE c VALUE cl_abap_char_utilities=>cr_lf.


DATA: lv_converter TYPE REF TO cl_abap_conv_in_ce,
lv_xstring  
TYPE xstring.



CASE iv_entity_name.
WHEN 'YourEntity'.

lv_xstring = is_media_resource-value.

lv_converter
= cl_abap_conv_in_ce=>create( input       = lv_xstring
encoding    = 'UTF-8'
replacement
= '?'
ignore_cerr
= abap_true ).

TRY.
CALL METHOD lv_converter->read( IMPORTING data = lv_output ).
CATCH cx_sy_conversion_codepage.
*-- Should ignore errors in code conversions
CATCH cx_sy_codepage_converter_init.
*-- Should ignore errors in code conversions
CATCH cx_parameter_invalid_type.
CATCH cx_parameter_invalid_range.
ENDTRY.

REPLACE gv_spl IN lv_output WITH ''.
      
SPLIT lv_output AT gv_spl INTO TABLE lv_out_tab.


LOOP AT lv_out_tab INTO ls_output.


REPLACE ALL OCCURRENCES OF cl_abap_char_utilities=>cr_lf(1) IN ls_output WITH ''.
SPLIT ls_output AT ',' INTO lv_mar-matnr ls_char lv_mar-ernam ls_char1 lv_mar-aenam lv_mar-vpsta lv_mar-pstat lv_mar-mtart lv_mar-matkl lv_mar-meins.

lv_mar
-matnr(1) = ''.
CONDENSE lv_mar-matnr.


IF ls_char IS NOT INITIAL.
CALL FUNCTION 'CONVERT_DATE_TO_INTERNAL'
EXPORTING
date_external                 
= ls_char
*             ACCEPT_INITIAL_DATE            =
IMPORTING
date_internal                 
= lv_mar-ersda
*           EXCEPTIONS
*             DATE_EXTERNAL_IS_INVALID       = 1
*             OTHERS                         = 2
.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
ENDIF.

IF ls_char1 IS NOT INITIAL.
CALL FUNCTION 'CONVERT_DATE_TO_INTERNAL'
EXPORTING
date_external                 
= ls_char1
*             ACCEPT_INITIAL_DATE            =
IMPORTING
date_internal                 
= lv_mar-laeda
*           EXCEPTIONS
*             DATE_EXTERNAL_IS_INVALID       = 1
*             OTHERS                         = 2
.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
ENDIF.



IF lv_mar-matnr IS NOT INITIAL.
APPEND lv_mar TO it_mara.
ENDIF.
CLEAR lv_mar.

ENDLOOP.

MODIFY zar_mara FROM TABLE it_mara.

ENDCASE.

17 Comments
Labels in this area