Below is 2 ABAP programs for downloading data to a file on the presentation server(PC). It also checks if file exists and
allows user to replace existing file, change name or cancel during download process.
Program 1 - (Replace existing file)
This method of file download with check uses the latest techniques and achieves a very neat solution.
REPORT ZPOPUP_REPLACE. DATA: ld_filename TYPE string, ld_path TYPE string, ld_fullpath TYPE string, ld_result TYPE i. types: begin of t_datatab , row(500) type c, end of t_datatab. data: it_datatab type STANDARD TABLE OF t_datatab, wa_datatab like line of it_datatab. wa_datatab-row = 'sdsds'. append wa_datatab to it_datatab. * Display save dialog window CALL METHOD cl_gui_frontend_services=>file_save_dialog EXPORTING * window_title = ' ' DEFAULT_EXTENSION = 'XLS' default_file_name = 'accountsdata' INITIAL_DIRECTORY = 'c:\temp\' CHANGING filename = ld_filename path = ld_path fullpath = ld_fullpath user_action = ld_result. * Check user did not cancel request CHECK ld_result EQ '0'. CALL FUNCTION 'GUI_DOWNLOAD' EXPORTING filename = ld_fullpath filetype = 'ASC' * APPEND = 'X' write_field_separator = 'X' * CONFIRM_OVERWRITE = 'X' TABLES data_tab = it_datatab[] "need to declare and populate EXCEPTIONS file_open_error = 1 file_write_error = 2 OTHERS = 3.
Program 2 - (Replace or Append data to existing file)
This file download program uses older techniques but achieves a perfectly acceptable solution which
also allows the user to append data to an existing file.
REPORT ZPOPUP_APPEND. * Internal table to store export data DATA: begin of it_excelfile occurs 0, row(500) type c, end of it_excelfile. DATA: rc TYPE sy-ucomm, ld_answer TYPE c, ld_filename type string. PARAMETERS: p_file like rlgrap-filename default 'C:\TEST'. it_excelfile-row = 'sdsds'. append it_excelfile. CALL FUNCTION 'WS_QUERY' EXPORTING query = 'FE' "File Exist? filename = p_file IMPORTING return = rc. ld_filename = p_file. IF rc NE 0. "If File alread exists CALL FUNCTION 'POPUP_TO_CONFIRM' EXPORTING * TITLEBAR = ' ' * DIAGNOSE_OBJECT = ' ' text_question = 'File Already exists!!' text_button_1 = 'Replace' * ICON_BUTTON_1 = ' ' text_button_2 = 'New name' * ICON_BUTTON_2 = ' ' * DEFAULT_BUTTON = '1' * DISPLAY_CANCEL_BUTTON = 'X' * USERDEFINED_F1_HELP = ' ' * START_COLUMN = 25 * START_ROW = 6 * POPUP_TYPE = IMPORTING answer = ld_answer * TABLES * PARAMETER = EXCEPTIONS text_not_found = 1 OTHERS = 2. * Option 1: Overwrite ********************* IF ld_answer EQ '1'. CALL FUNCTION 'GUI_DOWNLOAD' EXPORTING * BIN_FILESIZE = filename = ld_filename "File Name filetype = 'ASC' * IMPORTING * FILELENGTH = TABLES data_tab = it_excelfile "Data table EXCEPTIONS file_write_error = 1 no_batch = 2 gui_refuse_filetransfer = 3 invalid_type = 4 OTHERS = 5. IF sy-subrc <> 0. MESSAGE i003(zp) WITH 'There was an error during Excel file creation'(200). exit. "Causes short dump if removed and excel document was open ENDIF. * Option 2: New name. ********************* ELSEIF ld_answer EQ '2'. CALL FUNCTION 'DOWNLOAD' EXPORTING filename = p_file "File name filetype = 'ASC' "File type * col_select = 'X' "COL_SELECT * col_selectmask = 'XXXXXXXXXXXXXXXXXXXXXXXXXX' * "COL_SELECTMASK filetype_no_show = 'X' "Show file type selection? * IMPORTING * act_filename = filename_dat TABLES data_tab = it_excelfile "Data table * fieldnames = EXCEPTIONS file_open_error = 01 file_write_error = 02 invalid_filesize = 03 invalid_table_width = 04 invalid_type = 05 no_batch = 06 unknown_error = 07. ENDIF. ELSE. "File does not alread exist. CALL FUNCTION 'GUI_DOWNLOAD' EXPORTING * BIN_FILESIZE = filename = ld_filename "File name filetype = 'ASC' "File type * IMPORTING * FILELENGTH = TABLES data_tab = it_excelfile "Data table EXCEPTIONS file_write_error = 1 no_batch = 2 gui_refuse_filetransfer = 3 invalid_type = 4 OTHERS = 5. IF sy-subrc <> 0. MESSAGE i003(zp) WITH 'There was an error during Excel file creation'(200). exit. "Causes short dump if removed and excel document was open ENDIF. ENDIF.