Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

SAPUI5 Upload File response content from oData issues

Hi all,

I am developing a SAPUI5 in order to upload files [mainly text files ] for freight orders.
I have implemented the view using XML within the WebIDE as well as the corresponding JS controller,
which is calling an oData service matched with 'create_stream' method then doing the job of reading the file content.


All is working fine but then I cannot receive the response containing the file content [parsed] from the oData back to my js controller.

Here is my ajax call, actually there are two calls but the first one is used to get the necessary security csrf token only.

jQuery.ajax({

                    url : Service1,

                    type : "GET",

                    async: false,

                    beforeSend : function(xhr) {

                      xhr.setRequestHeader("X-CSRF-Token", "Fetch");

                    },

                    success : function(data, textStatus, XMLHttpRequest) {

                      token = XMLHttpRequest.getResponseHeader("X-CSRF-Token");

                    }

                  });

                  $.ajaxSetup({

                    cache : false

                  });

                  jQuery.ajax({

                    url : service_url,

                    async : false,

                    dataType : "text",

                    cache : false,

                    data : filedata,

                    type : "POST",

                    beforeSend : function(xhr) {

                      xhr.setRequestHeader("X-CSRF-Token", token);

                      xhr.setRequestHeader("Content-Type", "application/text;charset=UTF-8");

                    },

                    success : function(odata) {

                      oDialog.setTitle("File Uploaded");

                      oDialog.open();

                      document.location.reload(true);

                    },

                    error : function(odata) {

                      oDialog.setTitle("File NOT Uploaded");

                      oDialog.open();

                      document.location.reload(true);

                    }

                  });

Can anyone find where I am wrong within this flow ?


I have attached here the code for the controller and the create_stream method from the oData service plus a few lines for the ajax call doing the upload request.
I think the problem might be in this ajax call, maybe in the parameters or in the way I am getting the data as response ?

Or the point could be whithin the oData create_stream method ?

Thanks in advance for the help,

Ajax_call.PNG (25716 B)
Tags:
replied

Hi Claudia,

1. Try this code for returning the response:

data lr_entity TYPE REF TO data.

  FIELD-SYMBOLS: <fs1> TYPE any.

  CREATE DATA lr_entity type zcl_zgw_upload_file_mpc=>ts_file.

  ASSIGN lr_entity->* to <ls_data>.

  ASSIGN COMPONENT 'EMP_ID' OF STRUCTURE <ls_data> to <fs1>.

  <fs1> = 'my empid'.

  ASSIGN COMPONENT 'FILE_NAME' OF STRUCTURE <ls_data> to <fs1>.

  <fs1> = 'my file name'.

  copy_data_to_ref( EXPORTING is_data = <ls_data>

      CHANGING cr_data = er_entity ).

Here 'EMP_ID', 'FILE_NAME' are ABAP field names of properties: 'EmpId' and 'FileName' in my entity 'File', you can check the response in gateway client as below:

2.  for the approach:

b) Save the data parsed from the file in an attribute of the implementation class of create_stream's oData

     c)  When I receive the response in the Ajax call from create_stream method, immediately I call another ajax object pointing to another oData service in order to read the data saved in point b).
     d) The response of this Ajax call will be drawing into my webpage.

I would suggest:

b) save the xstring value in a transparent table in SAP. and return the FileID to UI as a response of create_stream call.

c) in the success function of the above call: capture the FileID and fire another ajax call(QUERY) to get the list of values using above FileID as filter. The call would be something like:

..../sap/ZMYSERVICE/FileSet?$filter=FileID eq '1234'

Now in the GET_ENTITYSET method of FileSet entityset, select the xstring saved in the table -> retrieve the data -> delete the table entry -> send the retrieved data as http response as you send the response of normal get_entityset.

d) yes, the response of above call is your final data and you can display that in UI.

Let me know if you face any issue.

Regards,

Ekansh

1 View this answer in context
Not what you were looking for? View more on this topic or Ask a question