Purpose
Transfer a binary file
Solution

GuiXT does not provide direct support for binary files, since in the internal representation all GuiXT variables are strings which are terminated by a 0-byte. To transfer binary files, e.g. a .png image file or a PDF, it is best to use a text format of the file.

In order to achieve the character format, you may use JavaScript, VB.NET or C#. A conversion to hex code, e.g. "3100" for the "1" and a 0-byte, or the Base64 format commonly used in the web environment, are possible.

Alternatively, you can read the file with CopyText and the -hexadecimal option and then use GuiXT means to convert it into a table with fixed length rows (see variant 2 below).

 Example
As an example we consider the following application:
The user selects a file on his PC. This is to be transferred to a function module in the SAP system, which then saves the file, for example, as an order attachment or in the MIME repository.

Our solution is as follows:

  • Selection of the file name via an InputScript with SelectFile
  • Calling a JavaScript routine with passing the file name. The JavaScript routine reads the file, creates a hexadecimal text format and fills a GuiXT table variable with it
  • Calling the function module via the "Open Call" interface. We pass the file content in text format and the desired name in the MIME repository
  • In ABAP, we convert the text format back into binary form and save the file.

The code below does not contain any error handling, e.g. handling missing authorizations to store the object in the MIME repository.

GuiXT InputScript, variant 1 (with JavaScript call)

SelectFile  filter="*.pdf" directory="F:\temp" name="pdfname"

if Q[ok]

  // create a table variable for the file content

  CreateTable V[content] line

  // Call up JavaScript to read the file
  // and fill the table variable

  CallJS filetohex "&V[pdfname]" "content.line"

  // Object name for MIME repository
  // path for this example is fixed SAP/PUBLIC/GUIXT/DEMO

  Set V[objectname] "test3.pdf"  

  // Call up the ABAP function module

  // the dialog flag is only needed if the called function
  // performs any dialog on its own
  // here the package selection popup for new MIME repository objects

   call /guixt/uploadfile  -dialog _

     export.objectname="objectname" _

     export.content="content"

  // final message

  Message "File &V[pdfname] stored in MIME repository" -statusline

endif

 

 
JavaScript function
function filetohex(filename, linename) {

    var a = binaryFileToArray(filename);

    var str = "";

    for (var k = 0; k < a.length; k++) {
        if (a[k] < 16) {
            str += '0' + a[k].toString(16);
        }
        else {
            str += a[k].toString(16);
        };
    };

    str = str.toUpperCase();

    var k = 0;
    var linesize = 4000;
    while (k * linesize < str.length) {

        guixt.set(linename + "." + (k + 1), 
            str.substr(k * linesize, linesize))
        k += 1;

    };

}

//Reads a binary file, returns an array
function binaryFileToArray(fileName) {
    var binStream = guixt.CreateObject("ADODB.Stream");
    binStream.Type = 1; // Type Binary
    binStream.Open();

    binStream.loadFromFile(fileName);

    var binVariant = binStream.read();
 
     binStream.Close();
    return VBArray(binVariant).toArray();

};
 
ABAP function module
function /guixt/uploadfile.

  data: input  type string,
        output type  xstring.

  concatenate lines of content into input.

  output = input.

  data: path type string.
  path ='/SAP/PUBLIC/GUIXT/DEMO/' && objectname.

  data(sapmr) = cl_mime_repository_api=>get_api( ).

  sapmr->put( i_url = path
              i_content = output ).

endfunction.
 

The function  module is not remarked as "RFC enabled" in order to allow strings in the interface. The Open Call interface allows us to call up modules which formally are not RFC-enabled in the function library.


GuiXT InputScript,  variant 2 (without JavaScript call)

SelectFile  filter="*.pdf" directory="F:\temp" name="pdfname"

if Q[ok]

  CopyText fromFile="&V[pdfname]" toText="content" -hexadecimal

  CopyText fromText="content" toString="filecontent"  

  CreateStructure V[contentline] line

  CreateTable V[content] line 

  Set V[index1] 1

  Set V[index2] 4000

  label next_line

  Set V[contentline.line] "&V[filecontent](&V[index1]-&V[index2])"

  if V[contentline.line]

    AppendRow V[contentline] table=V[content]

    Set V[index1] &V[index1] + 4000

    Set V[index2] &V[index2] + 4000

    goto next_line

  endif

 

  Set V[objectname] "test3.pdf"  

  // Call up the ABAP function module

  // the dialog flag is only needed if the called function
  // performs any dialog on its own
  // here the package selection popup for new MIME repository objects

   call /guixt/uploadfile  -dialog _

     export.objectname="objectname" _

     export.content="content"

  // final message

  Message "File &V[pdfname] stored in MIME repository" -statusline

endif

 





Components
InputAssistant  (+ Controls for Variant 1)