Monday 13 October 2014

UPLOADING AND DOWNLOADING FILE FROM PRESENTATION SERVER IN SAP

REPORT ztestr_file_handling.

PARAMETERS : p_path TYPE dxfields-longpath,
p_down TYPE dxfields-longpath.

TYPES : BEGIN OF ty_final,
field1(4) TYPE c,
field2(4) TYPE n,
END OF ty_final.

DATA : git_string TYPE STANDARD TABLE OF string INITIAL SIZE 1,
git_final TYPE STANDARD TABLE OF ty_final INITIAL SIZE 1.

*-- F4 help for the presentation layer
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_path.

CALL FUNCTION 'F4_DXFILENAME_TOPRECURSION'
EXPORTING
i_location_flag = 'P'
* i_server = space
* i_path = lv_i_path
* FILEMASK = '*.*'
* FILEOPERATION = 'R'
IMPORTING
* O_LOCATION_FLAG =
* O_SERVER =
o_path = p_path
* ABEND_FLAG =
EXCEPTIONS
rfc_error = 1
error_with_gui = 2
OTHERS = 3.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.

*-- F4 help for the presentation layer
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_down.

CALL FUNCTION 'F4_DXFILENAME_TOPRECURSION'
EXPORTING
i_location_flag = 'P'
* i_server = space
* i_path = lv_i_path
* FILEMASK = '*.*'
* FILEOPERATION = 'R'
IMPORTING
* O_LOCATION_FLAG =
* O_SERVER =
o_path = p_down
* ABEND_FLAG =
EXCEPTIONS
rfc_error = 1
error_with_gui = 2
OTHERS = 3.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.

START-OF-SELECTION.

IF p_path IS NOT INITIAL.
*-- Validate whether path exists or not
PERFORM validate_path.
*-- Upload the file
PERFORM upload_file.

IF git_string[] IS NOT INITIAL.

*-- Populate the internal table for processing
PERFORM populate_itab .

IF git_final[] IS NOT INITIAL.
*-- Process the file
PERFORM process_file.

IF p_down IS NOT INITIAL.

PERFORM validate_path_d.

*-- Download the file
PERFORM download_file.
ENDIF.


ENDIF.

ENDIF.
ENDIF.

*&---------------------------------------------------------------------*
*& Form VALIDATE_PATH
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM validate_path .

DATA: lv_result TYPE char1,
lv_file TYPE string.

lv_file = p_path.

CALL METHOD cl_gui_frontend_services=>file_exist
EXPORTING
file = lv_file
RECEIVING
result = lv_result
EXCEPTIONS
cntl_error = 1
error_no_gui = 2
wrong_parameter = 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.
IF lv_result NE 'X'." if lv_result = 'X' then file exists
MESSAGE 'File does not exist' TYPE 'I'.
EXIT.

ENDIF.

ENDFORM. " VALIDATE_PATH
*&---------------------------------------------------------------------*
*& Form UPLOAD_FILE
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM upload_file .

DATA : lv_filename TYPE string.

lv_filename = p_path.

CALL METHOD cl_gui_frontend_services=>gui_upload
EXPORTING
filename = lv_filename
* filetype = 'ASC'
* has_field_separator = SPACE
* header_length = 0
* read_by_line = 'X'
* dat_mode = SPACE
* codepage = SPACE
* ignore_cerr = ABAP_TRUE
* replacement = '#'
* virus_scan_profile =
* IMPORTING
* filelength =
* header =
CHANGING
data_tab = git_string
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
not_supported_by_gui = 17
error_no_gui = 18
OTHERS = 19
.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.




ENDFORM. " UPLOAD_FILE
*&---------------------------------------------------------------------*
*& Form POPULATE_ITAB
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM populate_itab .

DATA : lwa_final TYPE ty_final.
FIELD-SYMBOLS : <fwa_string> TYPE string.

LOOP AT git_string ASSIGNING <fwa_string>.

SPLIT <fwa_string> AT '|' INTO lwa_final-field1
lwa_final-field2.

APPEND lwa_final TO git_final.
CLEAR lwa_final.

ENDLOOP.

ENDFORM. " POPULATE_ITAB
*&---------------------------------------------------------------------*
*& Form PROCESS_FILE
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM process_file .

FIELD-SYMBOLS : <fwa_final> TYPE ty_final.

LOOP AT git_final ASSIGNING <fwa_final>.

<fwa_final>-field2 = <fwa_final>-field2 + 1.
CONDENSE <fwa_final>-field2.

ENDLOOP.

ENDFORM. " PROCESS_FILE
*&---------------------------------------------------------------------*
*& Form DOWNLOAD_FILE
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM download_file .

DATA : lv_filename TYPE string,
lwa_string TYPE string,
lit_string TYPE STANDARD TABLE OF string INITIAL SIZE 1.

FIELD-SYMBOLS : <fwa_final> TYPE ty_final.

IF p_down IS NOT INITIAL.

lv_filename = p_down.

IF git_final[] IS NOT INITIAL.

LOOP AT git_final ASSIGNING <fwa_final>.

CONCATENATE <fwa_final>-field1 <fwa_final>-field2 INTO lwa_string
SEPARATED BY '|'.
APPEND lwa_string TO lit_string.
CLEAR lwa_string.

ENDLOOP.

ENDIF.

CALL METHOD cl_gui_frontend_services=>gui_download
EXPORTING
* bin_filesize =
filename = lv_filename
* filetype = 'ASC'
* append = SPACE
* write_field_separator = SPACE
* header = '00'
* trunc_trailing_blanks = SPACE
* write_lf = 'X'
* col_select = SPACE
* col_select_mask = SPACE
* dat_mode = SPACE
* confirm_overwrite = SPACE
* no_auth_check = SPACE
* codepage = SPACE
* ignore_cerr = ABAP_TRUE
* replacement = '#'
* write_bom = SPACE
* trunc_trailing_blanks_eol = 'X'
* wk1_n_format = SPACE
* wk1_n_size = SPACE
* wk1_t_format = SPACE
* wk1_t_size = SPACE
* show_transfer_status = 'X'
* fieldnames =
* write_lf_after_last_line = 'X'
* virus_scan_profile = '/SCET/GUI_DOWNLOAD'
* IMPORTING
* filelength =
CHANGING
data_tab = lit_string
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
not_supported_by_gui = 22
error_no_gui = 23
OTHERS = 24
.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.

ENDIF.

ENDFORM. " DOWNLOAD_FILE
*&---------------------------------------------------------------------*
*& Form VALIDATE_PATH_D
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM VALIDATE_PATH_D .

DATA: lv_result TYPE char1,
lv_file TYPE string.

lv_file = p_down.

CALL METHOD cl_gui_frontend_services=>file_exist
EXPORTING
file = lv_file
RECEIVING
result = lv_result
EXCEPTIONS
cntl_error = 1
error_no_gui = 2
wrong_parameter = 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.
IF lv_result NE 'X'." if lv_result = 'X' then file exists
MESSAGE 'File does not exist' TYPE 'I'.
EXIT.

ENDIF.
ENDFORM. " VALIDATE_PATH_D


INPUT file contents

ABCD|1000
EFGH|2000
IJKL|3000
MNOP|4000
QRST|5000

OUTPUT FILE CONTENT :
ABCD|1001
EFGH|2001
IJKL|3001
MNOP|4001
QRST|5001
INPUT :














No comments:

Post a Comment