0

I have created an ICF handler class which sends files to the sender. The thing is, it works fine with single file where i am reading the data in binary format and attaching the same in body part using set_data.

But when I try to add more than 1 file, I am unable to add 2 files separately. i am using IF_HTTP_EXTENSION and do not have NTW GATEWAY component yet.

I am also using MULTIPART feature, but dont konw exactly on how to add 2 files separately. Can you please help me ?

//file1
server->response->set_header_field( name = 'Content-Type' value = 'multipart/mixed').
CONCATENATE 'form-data;name="file"; filename="' filename+5(9) '"' INTO lv_header_value.
server->response->set_header_field( name = 'content-disposition' value = lv_header_value ).
server->response->set_data( data = attach_xstring ).

//file2
server->response->add_multipart( ).
CONCATENATE 'form-data;name="file"; filename="' filename+5(9) '"' INTO lv_header_value.
server->response->set_header_field( name = 'content-disposition' value = lv_header_value ).
server->response->set_data( data = attach_xstring ).
Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
kjyothiraditya
  • 33
  • 1
  • 10
  • There may be some answers in the same question in [SCN](https://answers.sap.com/questions/12706388/send-more-than-1-file-in-http-response.html) (currently there's one) – Sandra Rossi Jun 11 '19 at 12:15
  • As I said in SCN, the response is built perfectly from ABAP side (as far as I can see), but probably it is not well decoded by your client. Please explain how you decode and render the response. – Sandra Rossi Jun 11 '19 at 12:20
  • Hi Sandra, I am testing it with web browser. – kjyothiraditya Jun 12 '19 at 04:07
  • and I am using Chrome Ver 74 – kjyothiraditya Jun 12 '19 at 04:13
  • Your suggestions seems to be correct, but i dont know why it is not working for me . Will check and update details. Also, for content-disposition, it says the files would be combined. – kjyothiraditya Jun 12 '19 at 04:37
  • If you use a web browser, then the download doesn't support multipart files as said [here](https://stackoverflow.com/questions/1806228/browser-support-of-multipart-responses). Instead, use a client which supports that kind of file. Or simply compress your files into a unique ZIP file that you return in the response. – Sandra Rossi Jun 12 '19 at 10:09

1 Answers1

1

You need to use add_multipart() method. Try like this:

      cl_http_client=>create( EXPORTING host = host service = port scheme = scheme
                              IMPORTING client = lo_http_client ).
      lo_http_client->request->set_header_field( name  = 'Content-Type' value = 'multipart/form-data' ). "#EC NOTEXT
      lo_request_part = lo_http_client->request->add_multipart( ).
      lo_request_part->set_content_type( 'application/xml' ).
      lv_content_disposition = |form-data; name="item"; filename="item_data.xml" |.
      lo_request_part->set_header_field( name = `Content-Disposition` value = lv_content_disposition ).
      lo_request_part->set_data( data = lv_create_item_xml ).

      LOOP AT mt_files ASSIGNING <attachment>.
        lo_request_part = lo_http_client->request->add_multipart( ).
        lo_request_part->set_content_type( <attachment>-content_type ). "#EC NOTEXT
        lv_content_disposition =  |form-data; name="{ <attachment>-part_name }"; filename="{ <attachment>-filename }" |.
        lo_request_part->set_header_field( name = `Content-Disposition` value = lv_content_disposition ).
        lo_request_part->set_data( <attachment>-file ).
      ENDLOOP.

It is sample for request, but for response the scheme should be the same. Here initially xml-file added to request and them multiple attachments are processed in loop.

Suncatcher
  • 10,355
  • 10
  • 52
  • 90