Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

problem saving file using SO_ATTACHMENT_INSERT

0 Likes
1,766

Hello,

I'm trying to do something that should be simple, but somehow it is not working.

In a report, get a file from your pc using GUI_UPLOAD, save it using SO_ATTACHMENT_INSERT, retrieve it using SO_ATTACHMENT_READ and SO_CONTENT_FROM_KPRO_GET and finally save it back into the pc using GUI_DOWNLOAD.

I'm using png files because I'd like to save images. The file I get back is corrupted. It has some missing data and I don't know why. Can anyone with experience using those FM's maybe see what I'm doing wrong? Thanks a lot in advance.

Regards,

Joaquin

Here is the source code. Copy-paste into a new report should work.

CONSTANTS lc_general  TYPE sofd-folrg  VALUE 'B'.

DATA: lt_content      TYPE TABLE OF soli,
      lt_header       TYPE TABLE OF soli,
      ls_content      TYPE soli,
      lv_filename     TYPE string,
      lt_content_rec  TYPE TABLE OF soli,
      lt_header_rec   TYPE TABLE OF soli,
      lv_attach_id    TYPE soodk,
      ls_folder_id    TYPE soodk.

SELECTION-SCREEN BEGIN OF BLOCK 0001.

PARAMETERS:      p_datei  TYPE localfile LOWER CASE.

SELECTION-SCREEN END OF BLOCK 0001.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_datei.
  DATA:
    file_table  TYPE filetable,
    action TYPE i,
    rc TYPE sysubrc.

  FIELD-SYMBOLS:
    <file> TYPE file_table.

  CALL METHOD cl_gui_frontend_services=>file_open_dialog
    EXPORTING
      default_extension       = 'PNG'
    CHANGING
      file_table              = file_table
      rc                      = rc
      user_action             = action
    EXCEPTIONS
      file_open_dialog_failed = 1
      cntl_error              = 2
      error_no_gui            = 3
      not_supported_by_gui    = 4
      OTHERS                  = 5.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
               WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

  CHECK action = cl_gui_frontend_services=>action_ok.

  READ TABLE file_table INDEX 1 ASSIGNING <file>.

  p_datei = <file>.

START-OF-SELECTION.

  lv_filename = p_datei.

* upload file from laptop.
  CALL FUNCTION 'GUI_UPLOAD'
    EXPORTING
      filename                = lv_filename
      filetype                = 'BIN'
    TABLES
      data_tab                = lt_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 ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    EXIT.
  ENDIF.

* get folder to store the file
  CALL FUNCTION 'SO_FOLDER_ROOT_ID_GET'
    EXPORTING
      region                = lc_general  "General
    IMPORTING
      folder_id             = ls_folder_id
    EXCEPTIONS
      communication_failure = 1
      owner_not_exist       = 2
      system_failure        = 3
      x_error               = 4
      OTHERS                = 5.
  IF sy-subrc NE 0.
    "error message
    EXIT.
  ENDIF.

* store file
  CALL FUNCTION 'SO_ATTACHMENT_INSERT'
    EXPORTING
      object_id                  = ls_folder_id
      attach_type                = 'BIN'
    IMPORTING
      attach_id                  = lv_attach_id
    TABLES
      objcont                    = lt_content
      objhead                    = lt_header
    EXCEPTIONS
      active_user_not_exist      = 1
      object_type_not_exist      = 2
      operation_no_authorization = 3
      owner_not_exist            = 4
      parameter_error            = 5
      substitute_not_active      = 6
      substitute_not_defined     = 7
      x_error                    = 8
      system_failure             = 9
      communication_failure      = 10
      OTHERS                     = 11.
  IF sy-subrc <> 0.
    "error message
    EXIT.
  ENDIF.

* read file
  CALL FUNCTION 'SO_ATTACHMENT_READ'
    EXPORTING
      object_id                  = ls_folder_id
      attach_id                  = lv_attach_id
    TABLES
      objcont                    = lt_content_rec
      objhead                    = lt_header_rec
    EXCEPTIONS
      active_user_not_exist      = 1
      object_not_exist           = 2
      operation_no_authorization = 3
      owner_not_exist            = 4
      parameter_error            = 5
      substitute_not_active      = 6
      substitute_not_defined     = 7
      x_error                    = 8
      communication_failure      = 9
      system_failure             = 10
      OTHERS                     = 11.

  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    EXIT.
  ENDIF.

* read content of file
  CALL FUNCTION 'SO_CONTENT_FROM_KPRO_GET'
    TABLES
      objcont = lt_content_rec.
  IF sy-subrc <> 0.
    EXIT.
  ENDIF.

* download to laptop
  CALL FUNCTION 'GUI_DOWNLOAD'
    EXPORTING
      filename                = 'c:\test1.png'
      filetype                = 'BIN'
    TABLES
      data_tab                = lt_content_rec
    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 ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

Edited by: Joaquin Recio Huertas on Feb 15, 2011 2:08 PM

2 REPLIES 2
Read only

0 Likes
981

I'm sorry, I don't know why all the return characters are gone.

Try again:

Hello,

I'm trying to do something that should be simple, but somehow it is not working.

In a report, get a file from your pc using GUI_UPLOAD, save it using SO_ATTACHMENT_INSERT, retrieve it using SO_ATTACHMENT_READ and SO_CONTENT_FROM_KPRO_GET and finally save it back into the pc using GUI_DOWNLOAD.

I'm using png files because I'd like to save images. The file I get back is corrupted. It has some missing data and I don't know why. Can anyone with experience using those FM's maybe see what I'm doing wrong? Thanks a lot in advance.

Regards, Joaquin

first part of code:

CONSTANTS lc_general  TYPE sofd-folrg  VALUE 'B'.

DATA: lt_content      TYPE TABLE OF soli,
      lt_header       TYPE TABLE OF soli,
      ls_content      TYPE soli,
      lv_filename     TYPE string,
      lt_content_rec  TYPE TABLE OF soli,
      lt_header_rec   TYPE TABLE OF soli,
      lv_attach_id    TYPE soodk,
      ls_folder_id    TYPE soodk.

SELECTION-SCREEN BEGIN OF BLOCK 0001.

PARAMETERS:      p_datei  TYPE localfile LOWER CASE.

SELECTION-SCREEN END OF BLOCK 0001.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_datei.
  DATA:
    file_table  TYPE filetable,
    action TYPE i,
    rc TYPE sysubrc.

  FIELD-SYMBOLS:
    <file> TYPE file_table.

  CALL METHOD cl_gui_frontend_services=>file_open_dialog
    EXPORTING
      default_extension       = 'PNG'
    CHANGING
      file_table              = file_table
      rc                      = rc
      user_action             = action
    EXCEPTIONS
      file_open_dialog_failed = 1
      cntl_error              = 2
      error_no_gui            = 3
      not_supported_by_gui    = 4
      OTHERS                  = 5.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
               WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

  CHECK action = cl_gui_frontend_services=>action_ok.

  READ TABLE file_table INDEX 1 ASSIGNING <file>.

  p_datei = <file>.

START-OF-SELECTION.

  lv_filename = p_datei.

* upload file from laptop.
  CALL FUNCTION 'GUI_UPLOAD'
    EXPORTING
      filename                = lv_filename
      filetype                = 'BIN'
    TABLES
      data_tab                = lt_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 ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    EXIT.
  ENDIF.


* get folder to store the file
  CALL FUNCTION 'SO_FOLDER_ROOT_ID_GET'
    EXPORTING
      region                = lc_general  "General
    IMPORTING
      folder_id             = ls_folder_id
    EXCEPTIONS
      communication_failure = 1
      owner_not_exist       = 2
      system_failure        = 3
      x_error               = 4
      OTHERS                = 5.
  IF sy-subrc NE 0.
    "error message
    EXIT.
  ENDIF.

* store file
  CALL FUNCTION 'SO_ATTACHMENT_INSERT'
    EXPORTING
      object_id                  = ls_folder_id
      attach_type                = 'BIN'
    IMPORTING
      attach_id                  = lv_attach_id
    TABLES
      objcont                    = lt_content
      objhead                    = lt_header
    EXCEPTIONS
      active_user_not_exist      = 1
      object_type_not_exist      = 2
      operation_no_authorization = 3
      owner_not_exist            = 4
      parameter_error            = 5
      substitute_not_active      = 6
      substitute_not_defined     = 7
      x_error                    = 8
      system_failure             = 9
      communication_failure      = 10
      OTHERS                     = 11.
  IF sy-subrc <> 0.
    "error message
    EXIT.
  ENDIF.

Read only

0 Likes
981

second part:

* read file
  CALL FUNCTION 'SO_ATTACHMENT_READ'
    EXPORTING
      object_id                  = ls_folder_id
      attach_id                  = lv_attach_id
    TABLES
      objcont                    = lt_content_rec
      objhead                    = lt_header_rec
    EXCEPTIONS
      active_user_not_exist      = 1
      object_not_exist           = 2
      operation_no_authorization = 3
      owner_not_exist            = 4
      parameter_error            = 5
      substitute_not_active      = 6
      substitute_not_defined     = 7
      x_error                    = 8
      communication_failure      = 9
      system_failure             = 10
      OTHERS                     = 11.

  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    EXIT.
  ENDIF.

* read content of file
  CALL FUNCTION 'SO_CONTENT_FROM_KPRO_GET'
    TABLES
      objcont = lt_content_rec.
  IF sy-subrc <> 0.
    EXIT.
  ENDIF.

* download to laptop
  CALL FUNCTION 'GUI_DOWNLOAD'
    EXPORTING
      filename                = 'c:\test1.png'
      filetype                = 'BIN'
    TABLES
      data_tab                = lt_content_rec
    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 ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

Thanks a lot in advance!