Application Development 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: 
SAP Community Downtime Scheduled for This Weekend

Download XML to server.

memorex17
Member
0 Kudos
312
Hello Community, I am trying to use the same logic for downloading an XML file (to my PC) for downloading it to the server.

This is my code. The GUI DOWNLOAD works correctly, but the OPEN DATASET creates a corrupted file...


How can I do it? Can you help me?


DATA rest TYPE i.
rest = 0. filesize = 0.
DATA ld_rest LIKE wa_xfile.
LOOP AT tab_xfile INTO wa_xfile.
IF rest <> 0.
CONCATENATE ld_rest(rest) wa_xfile
INTO wa_xfile IN BYTE MODE.
ENDIF.
* concatenate
rest = XSTRLEN( wa_xfile ).
WHILE rest >= x_linesize. " 128
wa_x = wa_xfile(x_linesize).
APPEND wa_x TO tab_x.
filesize = filesize + x_linesize.
SHIFT wa_xfile BY x_linesize PLACES IN BYTE MODE.
rest = rest - x_linesize.
ENDWHILE.
ld_rest = wa_xfile.
* this condition is now always true:
* rest_ = 0 or rest < x_linesize and
* contents in ld_rest(rest).
ENDLOOP.
IF rest > 0.
wa_x = wa_xfile(rest).
filesize = filesize + rest.
APPEND wa_x TO tab_x.
ENDIF.

* gui_download of tab_x

filename_string = filename.

IF ok_code NE 'SERV'.
CALL METHOD cl_gui_frontend_services=>gui_download
EXPORTING
bin_filesize = filesize
filename = filename_string
filetype = 'BIN'
no_auth_check = 'X'
IMPORTING
filelength = after_download_length
CHANGING
data_tab = tab_x
EXCEPTIONS
file_not_found = 91
file_write_error = 92
filesize_not_allowed = 93
invalid_type = 95
no_batch = 96
OTHERS = 97.
hlp_file_exit = filename.
IF sy-subrc NE 0. "download error
result = sy-subrc.
ELSEIF after_download_length EQ 0. "nothing written
result = 97.
ENDIF.
ELSE.
*SERVER

OPEN DATASET filename_string FOR OUTPUT IN BINARY MODE.

IF sy-subrc NE 0.
MESSAGE e208(00) WITH 'ERROR'.
ELSE.
LOOP AT tab_x INTO wa_x.
TRANSFER wa_x TO filename_string.
ENDLOOP.
CLOSE DATASET filename_string.
ENDIF.
ENDIF.
3 REPLIES 3

Sandra_Rossi
Active Contributor
0 Kudos
260

Please edit your question (Actions>Edit), select your code and press the button [CODE], which makes the code appear colored/indented, it'll be easier for people to look at it. Thanks!

Sandra_Rossi
Active Contributor
0 Kudos
260
DATA rest TYPE i.
rest = 0. filesize = 0.
DATA ld_rest LIKE wa_xfile.
LOOP AT tab_xfile INTO wa_xfile.
  IF rest <> 0.
    CONCATENATE ld_rest(rest) wa_xfile
    INTO wa_xfile IN BYTE MODE.
  ENDIF.
* concatenate
  rest = xstrlen( wa_xfile ).
  WHILE rest >= x_linesize. " 128
    wa_x = wa_xfile(x_linesize).
    APPEND wa_x TO tab_x.
    filesize = filesize + x_linesize.
    SHIFT wa_xfile BY x_linesize PLACES IN BYTE MODE.
    rest = rest - x_linesize.
  ENDWHILE.
  ld_rest = wa_xfile.
* this condition is now always true:
* rest_ = 0 or rest < x_linesize and
* contents in ld_rest(rest).
ENDLOOP.
IF rest > 0.
  wa_x = wa_xfile(rest).
  filesize = filesize + rest.
  APPEND wa_x TO tab_x.
ENDIF.

* gui_download of tab_x

filename_string = filename.

IF ok_code NE 'SERV'.
  CALL METHOD cl_gui_frontend_services=>gui_download
    EXPORTING
      bin_filesize         = filesize
      filename             = filename_string
      filetype             = 'BIN'
      no_auth_check        = 'X'
    IMPORTING
      filelength           = after_download_length
    CHANGING
      data_tab             = tab_x
    EXCEPTIONS
      file_not_found       = 91
      file_write_error     = 92
      filesize_not_allowed = 93
      invalid_type         = 95
      no_batch             = 96
      OTHERS               = 97.
  hlp_file_exit = filename.
  IF sy-subrc NE 0. "download error
    result = sy-subrc.
  ELSEIF after_download_length EQ 0. "nothing written
    result = 97.
  ENDIF.
ELSE.
*SERVER

  OPEN DATASET filename_string FOR OUTPUT IN BINARY MODE.

  IF sy-subrc NE 0.
    MESSAGE e208(00) WITH 'ERROR'.
  ELSE.
    LOOP AT tab_x INTO wa_x.
      TRANSFER wa_x TO filename_string.
    ENDLOOP.
    CLOSE DATASET filename_string.
  ENDIF.
ENDIF.

Sandra_Rossi
Active Contributor
0 Kudos
260

It's corrupted because you write a total number of bytes (maximum length of WA_X multiplied by integer number) which is probably > filesize (extra zeroes appended at the end of the file).

Your code is horrible, so I won't try to understand it.

In the end, you should obtain something like this, just write an XSTRING-type variable whose length should be equal to filesize, no loop needed:

  OPEN DATASET filename_string FOR OUTPUT IN BINARY MODE.
  TRANSFER xstring TO filename_string.
  CLOSE DATASET filename_string.