Skip to Content

How-to create a web service with attachments (SOAP With Attachments)

Suppose that a scenerio needs a way to send data to a remote service to do some specific work for you. To reach that goal, there are many ways; may be the most popular one in this SOA driven world is the web services. Think of a moment that you also need to send binary data with plain text ( for instance a pdf document, a jpg image, etc.) in this scenerio. What’s the right way, or what can you do if the only way is integrating with the web services.


You can send this binary data with the web service, more specifically, with what’s called SOAP with Attachments, sometimes referred as “web services with attachments” in the forums. And the good news is for String and byte[] parameters, you can use this feature with Netweaver Developer Studio and SAP Web AS.

Our little task is as follows :  

Scenerio: Sending product specification with a product image to the headquarters.
Implementation : SOAP with Attachments
Result : The specifications and the image(BLOB) is stored in the company’s database for later use.

Lets start with the web service generation: In my previous blog How-To Create Dynamic & Configurable Web Services Easily With Netweaver Development Components (Part I) I simply talked about creating a web service within minutes. Our approach will be the same with small differences. This time we go step by step to understand details. We start by creating an enterprise archive project. Then start to build our web service with building an EJB module project and adding an EJB(Stateless session bean) with method registerProducts.


This method takes two arguments : product name type: String which is our product specification (In real life scenerios you can add more features, implement them in java beans etc, for simplicity I will use only one property), and product image in binary(byte[]) format. I will return a “file saved” message with date and time if the operation is successfull.

The method simply gets the binary data from our web service and write to database.. Upps no, simple is good, to filesystem. (I won’t talk about writing it to database, there are many articles about this subject. (once you have your data as byte[] you can do anything).



Our method is as follows :

public String registerProduct(String productName, byte[] productImage) {
File f = new File("D:\tmp\"+productName);
FileOutputStream fos = new FileOutputStream(f);
return "file saved @"+new java.util.Date()+"";
catch (FileNotFoundException e) {
// XXX Auto-generated catch block
return "Error saving product";
catch (IOException e) { // XXX Auto-generated catch block
return "Error saving product;


Now for the web service part, first we need to create a virtual interface from our EJB and include our only method registerProduct.

The next thing we will do is to create a web service definition from the virtual interface. It will the base for our web service.


The last step is the web service deployment descriptor and adding a web service into this descriptor by selecting EJB, Web service definition we previously define and the name of the web service.

The most important part is adding a configuration, and the simple thing we should do is to create a transport binding with HTTP SOAP with Attachments. After creating this we should see Attachments property under our configuration, with our methods and parameters. That’s it. Our web service is ready to take binary data with SOAP with attachments protocol, and save it to filesystem or database.


One last thing to mention is to give necessary permissions to the folder you try to save the binary data.

For the sake of seeing the difference between the definitions of your web service and the others, just look at the Web Services navigator ( (put your server host and port)



In my next blog I will create a web service, that also takes binary data(without attachments), compare the services'(SOAP with and without attachments) performance with a simple testing program which uses standalone proxy.

You must be Logged on to comment or reply to a post.
  • Hi
       Baris, thanks for the excellent blog. My problem is how I can consume that web service (with attachment facility) from a web dynpro application. Previously I have accessed simpler web services (without attachment facility) from web dynpro application. But when I am importing (i.e creating model) that WS (with attachment facility), the wizard can’t show the Virtual Interface (VI). Is there any requirement of SOAP adapter to use these types of WS?
    • Sudip,
      Just start by creating an ordinary web service, and use the wsdl with style mime.

      By this way a logical port is created for you under web services, this is what you will use.

      Dont map the byte[] type to your controller or view context, just send it externally like this code because there’s a problem in this.

      For my attachment web service i created the code for you :

      byte[] mybytes = readFile("C:\\test.gif");
                SOAP_W_ATTACH_MODEL model = new SOAP_W_ATTACH_MODEL();
       service = model.getServiceImpl();
                ConfigWAttachmentBindingStub stub = (ConfigWAttachmentBindingStub) service.getLogicalPort(ConfigWAttachmentBindingStub.class);

      • Does it have to be a WebService (with attachments) created by SAP. I wanted to check whether SAP iview could consume a WebService with Attachments created by Third Party Applications like Siebel. Would you have any example?
  • Thinks for your document, but i want to know how to call
    the web service . I had write a class to  call the web service
    but it was failed.

    import javax.xml.namespace.QName;

    import org.apache.axis.client.Call;
    import org.apache.axis.client.Service;

    public class Testservice {

         public static void main(String[] args) throws Exception{
              String endpoint = ““;
              Service service = new Service();
              Call call = (Call)service.createCall();
              call.setOperationName(new QName(“getMessage”));
              Object returnValue =
                   call.invoke(new Object[] { new String(“ANDY”) });
              System.out.println((String) returnValue);

  • Hi,

    Thanks for the excellent blog.  I am fairly new to this, but our scenario is the same as your example.

    We are on 6.2 and our customer has an application that wants to call our SAP system with a web service and the result of that call are material information, with the binary data for the material.

    I have verified that 6.2 can create web services, but I need help in defining a new RFC enabled function that will send binary data.

    Any help is appreciated.


  • Hi,

    Your blog is really interesting and resourcefull.
    However i have a requirement where i need to send the attachment from a ABAP generated web service. Is it possible to do so or is it supported only from JAVA based web services usign EJBs etc. I am not conversant with Java so any help with ABAP side will be highly appreciated.