How to Store Files in Database in ABAP

There are requirements where documents has to be uploaded and associated with business data available in the system. When we store files in tables, it will be easy to link it with other tables data. This article is aimed at storing files in databases and downloading or uploading them.

There is a data type named RAWSTRING used for storing large binary data into database. RAWSTRING is the Data Dictionary equivalent of the ABAP type XSTRING. So you can assign an XSTRING variable to RAWSTRING variable and vice versa. We are going to create a Data Element with the data type RAWSTRING.

Data Element with RAWSTRING

Once the Data Element is created, create a Table with the following fields which is going to be used to store the files.

Table with RAWSTRING

Now we are going to create two programs, one for uploading file to the database table from the presentation server and another to download it back to the presentation server.

Uploading file to Database Table

The logic is simple as this, - Upload the file into an Internal Table using the Function Module GUI_UPLOAD - Convert the Internal Table to XSTRING - Assign the XSTRING to the RAWSTRING in the Work Area - Insert the Work Area into the database table

That's it! The following code does the same.

report  zupload.

  p_fname type string lower case.

      gs_store_file type zfar_store_file,
      gt_content type standard table of tdline,
      len type i,
      xstr_content type xstring.


  "Upload the file to Internal Table
  call function 'GUI_UPLOAD'
      filename                = p_fname
      filetype                = 'BIN'
      filelength              = len
      data_tab                = gt_content
      file_open_error         = 1
      file_read_error         = 2
      no_batch                = 3
      gui_refuse_filetransfer = 4
      invalid_type            = 5
      no_authority            = 6
      unknown_error           = 7
      bad_data_format         = 8
      header_not_allowed      = 9
      separator_not_allowed   = 10
      header_too_long         = 11
      unknown_dp_error        = 12
      access_denied           = 13
      dp_out_of_memory        = 14
      disk_full               = 15
      dp_timeout              = 16
      others                  = 17.

  if sy-subrc <> 0.
    message 'Unable to upload file' type 'E'.

  "Convert binary itab to xstring
  call function 'SCMS_BINARY_TO_XSTRING'
      input_length       = len
*     FIRST_LINE         = 0
*     LAST_LINE          = 0
     buffer             = xstr_content
      binary_tab         = gt_content
     failed             = 1
     others             = 2
  if sy-subrc <> 0.
    message 'Unable to convert binary to xstring' type 'E'.

  clear gs_store_file.

  gs_store_file-filename = p_fname.
  gs_store_file-file_content = xstr_content.

  "Insert file into table
  insert zfar_store_file from gs_store_file.

  if sy-subrc is initial.
    message 'Successfully uploaded' type 'S'.
    message 'Failed to upload' type 'E'.

The code when execute asks for path of a file to be uploaded and uploads the file into the table.

Upload Program Selection Screen

Downloading file from Database Table

This is going to be exact inverse of the uploading steps.

  • Select the file from the table into a Work Area
  • Assign the RAWSTRING file content to a XSTRING variable
  • Convert the XSTRING to Internal Table
  • Download the file to the presentation server using the Function Module GUI_DOWNLOAD
report  zdownload.

  p_fname type zfar_store_file-filename lower case,
  p_path type string lower case.

      gs_store_file type zfar_store_file,
      xstr_content type xstring,
      gt_content type standard table of tdline,
      len type i,
      str_fname type string.


  select single * from zfar_store_file
    into gs_store_file
    where filename = p_fname.

  xstr_content  = gs_store_file-file_content.

  "Convert xstring/rawstring to binary itab
  call function 'SCMS_XSTRING_TO_BINARY'
      buffer        = xstr_content
      output_length = len
      binary_tab    = gt_content.

  if sy-subrc <> 0.
    message 'Unable to convert xstring to binary'
      type 'E'.

  str_fname = p_fname.

  call function 'GUI_DOWNLOAD'
      bin_filesize            = len
      filename                = p_path
      filetype                = 'BIN'
      data_tab                = gt_content
      file_write_error        = 1
      no_batch                = 2
      gui_refuse_filetransfer = 3
      invalid_type            = 4
      no_authority            = 5
      unknown_error           = 6
      header_not_allowed      = 7
      separator_not_allowed   = 8
      filesize_not_allowed    = 9
      header_too_long         = 10
      dp_error_create         = 11
      dp_error_send           = 12
      dp_error_write          = 13
      unknown_dp_error        = 14
      access_denied           = 15
      dp_out_of_memory        = 16
      disk_full               = 17
      dp_timeout              = 18
      file_not_found          = 19
      dataprovider_exception  = 20
      control_flush_error     = 21
      others                  = 22.
  if sy-subrc <> 0.
    message 'Unable to download file from SAP'
      type 'E'.

And when executed,

Download Program Selection Screen

Here to keep the things simple for the demo I'm storing the name of the file with full path as the key in the database table. But this is not a good idea. It is better you have a proper unique key. Make sure you also store the file type (preferably MIME type of the file) in a separate column, so that you do not depend upon the filename to determine the filetype.

Fareez Ahamed

SAP Developer, Full Stack Web Developer, Laravel & Vue.js fanatic