quinta-feira, 15 de dezembro de 2022

Gravando conteúdo de arquivo em tabelas RAWSTRING


Fonte da informação: https://www.fareez.info/blog/how-to-store-files-in-database-in-abap/





 Gravando.

report  zupload.

parameters:
  p_fname type string lower case.

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

start-of-selection.

  "Upload the file to Internal Table
  call function 'GUI_UPLOAD'
    exporting
      filename                = p_fname
      filetype                = 'BIN'
    importing
      filelength              = len
    tables
      data_tab                = gt_content
    exceptions
      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'.
  endif.

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

  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'.
  else.
    message 'Failed to upload' type 'E'.
  endif.

Lendo.

report  zdownload.

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


data:
      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.

start-of-selection.

  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'
    exporting
      buffer        = xstr_content
    importing
      output_length = len
    tables
      binary_tab    = gt_content.
  .

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

  str_fname = p_fname.

  call function 'GUI_DOWNLOAD'
    exporting
      bin_filesize            = len
      filename                = p_path
      filetype                = 'BIN'
    tables
      data_tab                = gt_content
    exceptions
      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'.
  endif.