Skip to Content
Author's profile photo Amaresh Pani

File attachment in ABAP Web Service – Service Consumer(Consumer Proxy)

During one of our requirement I found ABAP Consumer proxy class doesn’t create the attachment method like Java consumer proxy. We need to insert the attachment at runtime using WS protocol. Here I am going to explain the steps and code.


  1. Web service you are going to consume has the attachment enabled

        <sapattahnd:Enabled xmlns:sapattahnd=”“>true</sapattahnd:Enabled>

  2.  Web service is consumed and proxy class is activated

  3.  Create a Z class, method and use the code below

METHOD set_proposal.
* Author: Amaresh Pani
  DATA: lcl_obj TYPE REF TO zca_co_customerproposal,
        ls_request  TYPE  zca_send_customer_proposal_re4,
        ls_response TYPE  zca_send_customer_proposal_re3.
      CREATE OBJECT lcl_obj.
      CATCH cx_ai_system_fault INTO lx_fault.
      gv_msg = lx_fault->get_text( ).
DATA: lo_attachments                TYPE REF TO if_wsprotocol_attachments.
DATA: g_attachment    TYPE REF TO  if_ai_attachment,
      g_attachments    TYPE          prx_attach,      "Internal table for attachment
      g_attach_xstring TYPE          xstring.          "variable of xstring type.
* binary xstring file stored in Z table
SELECT SINGLE value FROM zzupld INTO g_attach_xstring WHERE filename = 'CUST'.
    lo_attachments ?= lcl_obj->get_protocol( if_wsprotocol=>attachments ).
    g_attachment = lo_attachments->get_attachment_from_binary(
          data = g_attach_xstring                " File content
          type = if_ai_attachment=>c_mimetype_pdf " MIME type
          name = 'CustomerProposal' ).            " Name of PDF to be uploaded
    APPEND g_attachment TO g_attachments.
    lo_attachments->set_attachments( g_attachments ).
    CATCH cx_ai_system_fault INTO lx_fault.
    gv_msg = lx_fault->get_text( ).
ls_request-send_customer_proposal_request-document-opprotunity_id-content = '7117941'. " Defaulted content of proxy
          send_customer_proposal_request  = ls_request
          send_customer_proposal_respons = ls_response ).
    CATCH cx_ai_system_fault INTO lx_fault.
      gv_msg = lx_fault->get_text( ).
    CATCH cx_ai_application_fault INTO lx_fault1.
      gv_msg = lx_fault->get_text( ).

Content in Z table zzupld


Remember to set attachment “YES” while creating the endpoint.


Execute the method to trigger the web service with attachment. Enable payload trace in SRT_UTIL to see the file attachment.


SOAP creates tag MULTIPART. PART_1 contains the general SOAP xml content. PART_2 contains the attachment as above.

Assigned Tags

      You must be Logged on to comment or reply to a post.
      Author's profile photo Ravi Narravula
      Ravi Narravula

      Hi Amaresh,

      Thanks for the information.

      I have a requirement to save a file attachment in SAP using Web Service, File will come from client(PHP).

      Right now, I have a web service which deals with only data.

      Client(PHP) requests data to SAP by providing input data, SAP will return the particular data to client.

      In same Web Service, Client(PHP) will upload the file, That need to be save in SAP.

      Please provide the sample programs or scenarios to handle attachments in server(SAP) side .

      Best Rgards,

      Ravi Narravula


      Author's profile photo Scott Overmeyer
      Scott Overmeyer

      I was wondering if you figured out how to do this.

      I have a similar requirement.

      I have put a question out there - but have not heard anything back.




      Scott Overmeyer

      Author's profile photo Chitrasen Singh Oinam
      Chitrasen Singh Oinam

      where to enable this    <sapattahnd:Enabledxmlns:sapattahnd=”“>true</sapattahnd:Enabled>