6

My ABAP devs are sending me file through a function. I'm trying to find out if I can convert a file into a byte array in ABAP>.

If this is possible, does anyone have any example code?

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
Nate
  • 2,316
  • 4
  • 35
  • 53

2 Answers2

7

something like this should work:

data: w_line type xstring.
data: t_file type table of xstring.
data: w_filename type string falue 'myfile.txt'.
data: w_len type i.

open dataset w_filename for input in binary mode.

read dataset w_filename into w_line length w_len.

while w_len > 0.
    append w_line to t_file.
    read dataset w_filename into w_line length w_len.
endwhile.

close dataset w_filename.

* t_file now holds the data in an internal table
0

There's a handful of ways to do it, but I've found using Objects is easiest.

DATA byte_array TYPE TABLE OF raw256. "any type will work here
DATA my_file    TYPE string VALUE `C:\users\bob\file.bin`. "Absolute or relative works

CL_GUI_FRONTEND_SERVICES=>GUI_UPLOAD(
  EXPORTING
    filename = my_file
    filetype = 'BIN'
  CHANGING
    data_tab = byte_array ).

This class is very robust from my experience. There are a bunch of optional parameters and return codes. SAP wrote some great documentation here.

Eric
  • 1,511
  • 1
  • 15
  • 28