How to upload a file via ABAP RFC with Webdynpro ?

三世轮回 提交于 2020-01-01 02:09:43

Xiaopeng Tan

Posts: 32
Registered: 7/18/05
Forum Points: 6

How to upload a file via ABAP RFC with Webdynpro ?
Posted: Nov 23, 2005 10:45 PM



Reply

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?

Valery Silaev

Posts: 2,972
Registered: 3/3/04
Forum Points: 7,127

Re: How to upload a file via ABAP RFC with Webdynpro ?


Posted: Nov 24, 2005 10:15 AM  

in response to: Xiaopeng Tan



Reply

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

Xiaopeng Tan

Posts: 32
Registered: 7/18/05
Forum Points: 6

Re: How to upload a file via ABAP RFC with Webdynpro ?
Posted: Nov 24, 2005 11:51 AM  

in response to: Valery Silaev



Reply

Hi Valery,
That's exactly what I was looking for. It's working now. Perfect!
Thank you very much!
Best regards,
Xiaopeng

 

http://forums.sdn.sap.com/thread.jspa?threadID=84501

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!