cancel
Showing results for 
Search instead for 
Did you mean: 

How to upload a file via ABAP RFC with Webdynpro ?

0 Kudos

Hi everyone,

I have the follwing problem.

I`m currently creating a webdynpro DC to upload files to a mySAP CRM system via RFC.

The RF-module is already available at the backend side and I have imported it as a model. The ABAP module interface looks like this:


*"  IMPORTING
*"     VALUE(IV_CASE_GUID) TYPE  SCMG_CASE_GUID
*"     VALUE(IV_FILENAME) TYPE  SDOK_PROPV DEFAULT 'Case Document'
*"     VALUE(IV_MODELNODE) TYPE  STRINGVAL DEFAULT '19'
*"     VALUE(IV_FILE_CONTENT_BINARY) TYPE  SDOKCNTBINS OPTIONAL
*"  EXPORTING
*"     VALUE(EV_MESSAGE) TYPE  BAPI_MSG
*"  EXCEPTIONS
*"      UPLOAD_FAILED

The parameter IV_FILE_CONTENT_BINARY of TYPE SDOKCNTBINS is imported into the webdynpro model as input structure ( 0..n cardinality and of collectiontype list) with one child attribute (line) which can be fed with a byte array (data type RAW with length 1022 on ABAP side).

I`m not really familiar with ABAP. To my understanding the structure IV_FILE_CONTENT_BINARY takes lines of byte arrays and that list of arrays will be uploaded with the RFC, right?

Now in my Webdynpro view I implemented the standard FileUpload UI element from which I got a IWDResource object.

Reading the IWD Resource object I got a inputStream from the file I`d like to upload.

Now my question is: how to get the IV_FILE_CONTENT_BINARY list filled with the inputStream?

I at least figured out that reading a byte array and put it into the structure would pretty much look like the follwing:



//create new list object
Sdokcntbin bin = new Sdokcntbin();

//create a temporary byte array with length of the line to be inserted into the list.
byte[] b = new byte[bin.getLine().length]; 

**read the inputStream into the byte array**
inputStream.read(b); ???

//write the byte array into the list
bin.setLine(b);

//add list to input node for RFC
ModelNode.addIv_File_Content_Binary(bin);

Do I need to create a loop to read the file binary as byte array segments of length 1022 each or am I wrong here totally?

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Xiaopeng,

Yes, you have to read input stream by 1022 bytes chunk, and create Sdokcntbin per chunk.

Something like this:


final int chunkSize = 1022;
final byte[] chunk = new byte[chunkSize];
final BufferedInputStream in 
  = new BufferedInputStream(resource.getInputStream());
try
{
  int size = -1;
  while ( (size = in.read(chunk, 0, chunkSize)) > 0 ) 
  {
    // If last chunk is partial, pad rest with zeros
    if ( size < chunkSize )
      Arrays.fill( chunk, size, chunkSize, 0);

    //create new list object
    final Sdokcntbin bin = new Sdokcntbin();
    //write the byte array into the list
    bin.setLine(chunk);
    //add list to input node for RFC
    <ModelObject>_Input.addIv_File_Content_Binary(bin);
  }
}
finally
{
  in.close();
}

Valery Silaev

EPAM Systems

http://www.NetWeaverTeam.com

Message was edited by: Valery Silaev

0 Kudos

Hi Valery,

That's exactly what I was looking for. It's working now. Perfect!

Thank you very much!

Best regards,

Xiaopeng

shreyas_pandya
Contributor
0 Kudos

Hi Valery,

I have a similar situation

but I am using JCO Connections instead of models in my application.

My File Upload Elelment is bound to a Context attribute "FileResource"of type Resource(Dictionary Simple Type).

I am using one RFC to store this file

the import Parameter of RFC contains an associated type Line type, which is of type "RAW" of length1022.

name of the import parameter is "IV_FILE_ATTACH"

& the line type is "LINE".

I went through your code, I got some idea, but i got confused at the follwing line.

final BufferedInputStream in = new BufferedInputStream(resource.getInputStream());

How do i implement this in my scenario,

because in my code "resource object is created in the following way

IWDResource resource = element.getFileResource();

"FileResource" is the view context attribute "FileResource"of type Resource(Dictionary Simple Type).

so I am not able to get the method

resource.getInputStream()).

Kindly guide me.

Edited by: Shreyas Pandya on Feb 9, 2009 6:50 AM

Former Member
0 Kudos

Hi guys,

I came accross that code that seems really usefull in my case.

However I still have one issue as i'm working on NW04 and don't have Resource type (only available since NW04S).

So i end up by trying with a binary type. And so I have a byte array that I use like this


final BufferedInputStream in  = new BufferedInputStream(new ByteArrayInputStream(wdContext.currentContextElement().getFileContent()));

Is it the right way to work?

Or is there another (better) way to deal with my issue?

Thanks for any input or help!

Thomas

Stefan-EA
Contributor
0 Kudos

Hi guys,

>

> I came accross that code that seems really usefull in my case.

> However I still have one issue as i'm working on NW04 and don't have Resource type (only available since NW04S).

> So i end up by trying with a binary type. And so I have a byte array that I use like this

>


> final BufferedInputStream in  = new BufferedInputStream(new ByteArrayInputStream(wdContext.currentContextElement().getFileContent()));
> 

>

> Is it the right way to work?

> Or is there another (better) way to deal with my issue?

>

> Thanks for any input or help!

>

>

> Thomas

Take a look at this upload tutorial for NW04

http://www.sdn.sap.com/irj/sdn/nw-wdjava?rid=/webcontent/uuid/503f22f6-b978-2a10-bf97-ddc21267e752#4...

Answers (1)

Answers (1)

Former Member
0 Kudos

Xiaopeng Tan

I also got a similar requirement. Please let me know the SAP-RFC function module used.

Thanks,

Varma