Sunteți pe pagina 1din 14

Tools

Attachments (0) Page History Restrictions Info Link to this Page View Wiki Markup

Function Module to Upload and Download files from and to Application Server
Page restrictions apply Added by Keshav.T, last edited by Moshe Naveh on Sep 26, 2010 (view change) Comment: Function Module to Upload and Download files from and to Application Server

Summary
This function module can be used to Upload and Download files from/to application server. It must have field separator to be specified while doing the Upload/Download operations. This function module can be easily enhanced or modularized for other file operations like moving files, deleting files etc

Author(s):
Created on: 22.09.2010 Name: Kesavadas Thekkillath Company: Intelligroup - an NTT DATA Company

Function Module Importing parameters


Parameter IM_PATH IM_OPERATION Typing Associated type Description File path for TYPE LOCALFILE Upload/Download File Operation TYPE ABAP_CHAR1 Upload/Download TYPE CPCODEPAGE SAP Character Set ID TYPE ABAP_CHAR1 Field Separator TYPE ABAP_CHAR1 Dataset append mode ABAP_CHAR1 Authority check Optional No No Yes No Yes Yes

IM_CODEPAGE IM_SEPARATOR IM_APPEND IM_AUTHORITY_CHEC TYPE K

Tables parameters
Parameter Typing Associated type Description IT_DATA Table with data

Exceptions
Exception INVALID_INPUT FILE_OPERATION DYNAMIC_TABLE_GENERATION Short text Invalid Inputs File Operation Dynamic table Creation

Top Include
TYPE-POOLS:abap. CONSTANTS:c_upload TYPE abap_char1 VALUE 'U', c_download TYPE abap_char1 VALUE 'D', c_activity_read type char04 VALUE 'READ', c_activity_write type char05 value 'WRITE'. DATA:i_tab TYPE TABLE OF string, i_comp TYPE cl_abap_structdescr=>component_table. DATA:wf_string TYPE string, wf_obj TYPE REF TO cx_sy_file_open, wf_cx type ref to cx_root, wf_ltype TYPE c, wi_index TYPE i, wf_length TYPE i, wf_filename type FILEEXTERN, wf_maximum TYPE i, wi_count TYPE i, wf_total_fields type i, wf_infile TYPE REF TO data, wf_type_struct TYPE REF TO cl_abap_structdescr, wf_type_table TYPE REF TO cl_abap_tabledescr,

wf_data_tab wf_data_str

TYPE REF TO data, TYPE REF TO data.

DATA:wa_comp LIKE LINE OF i_comp, wa_fields type abap_compdescr. FIELD-SYMBOLS: <fs_line> <fs_field_s> TYPE ANY, TYPE ANY,

<fs_field_t> TYPE ANY, <fs_table> TYPE standard table,

<fs_wa>

TYPE ANY.

Source Code
FUNCTION z_file_upload_download. *"---------------------------------------------------------------------*"*"Local Interface: *" *" *" *" *" *" *" *" *" *" *" *" *" IMPORTING VALUE(IM_PATH) TYPE LOCALFILE ABAP_CHAR1 CPCODEPAGE ABAP_CHAR1

VALUE(IM_OPERATION) TYPE VALUE(IM_CODEPAGE) TYPE VALUE(IM_SEPARATOR) TYPE VALUE(IM_APPEND) TYPE

ABAP_CHAR1 OPTIONAL ABAP_CHAR1 OPTIONAL

VALUE(IM_AUTHORITY_CHECK) TYPE TABLES IT_DATA EXCEPTIONS INVALID_INPUT FILE_OPERATION DYNAMIC_TABLE_GENERATION

*"----------------------------------------------------------------------

CLEAR :wf_string,i_tab[]. IF im_append = abap_true AND im_operation = c_upload. MESSAGE e904 WITH 'Append mode with upload not allowed'(001) RAISING invalid_input. ENDIF. IF im_operation = c_upload. CLEAR it_data[]. ENDIF. IF it_data[] IS INITIAL AND im_operation = c_download. MESSAGE e904 WITH 'No data Provided to Download'(001) RAISING invalid_input. ENDIF. IF im_operation NE c_upload AND im_operation NE c_download. MESSAGE e904 WITH 'Invalid Operation'(002) RAISING invalid_input. ENDIF. IF im_authority_check = abap_true. wf_filename = im_path. IF im_operation = c_upload. wf_string = 'No Authorization to Read'(006). PERFORM check_authority USING c_activity_read wf_filename. ELSEIF im_operation = c_download. wf_string = 'No Authorization to Write'(007). PERFORM check_authority USING c_activity_write wf_filename. ENDIF. IF sy-subrc <> 0.

MESSAGE e904 WITH wf_string RAISING file_operation. ELSE. CLEAR wf_string. ENDIF. ENDIF. TRY. IF im_operation = c_upload. IF im_codepage = '4110'. OPEN DATASET im_path ERRORS. FOR INPUT IN TEXT MODE ENCODING DEFAULT IGNORING CONVERSION

ELSE. OPEN DATASET im_path FOR INPUT IN LEGACY TEXT MODE CODE PAGE im_codepage

IGNORING CONVERSION ERRORS. ENDIF. ELSEIF im_operation = c_download. IF im_codepage = '4110'. IF im_append = abap_true. OPEN DATASET im_path FOR APPENDING IN TEXT MODE ENCODING DEFAULT IGNORING CONVERSION ERRORS. ELSE. OPEN DATASET im_path FOR OUTPUT IN TEXT MODE ENCODING DEFAULT IGNORING CONVERSION ERRORS. ENDIF. ELSE. IF im_append = abap_true.

OPEN DATASET im_path FOR APPENDING IN LEGACY TEXT MODE CODE PAGE im_codepage

IGNORING CONVERSION ERRORS. ELSE. OPEN DATASET im_path FOR OUTPUT IN LEGACY TEXT MODE CODE PAGE im_codepage

IGNORING CONVERSION ERRORS. ENDIF. ENDIF. ENDIF. CATCH cx_sy_file_open INTO wf_obj. PERFORM exp_txt USING wf_obj. MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4 RAISING file_operation. ENDTRY. CREATE DATA wf_infile LIKE LINE OF it_data. ASSIGN wf_infile->* TO <fs_line>. CLEAR:wf_ltype,wi_count. DESCRIBE FIELD <fs_line> TYPE wf_ltype

COMPONENTS wi_count. wf_maximum = 0. wf_total_fields = wi_count. WHILE wi_count GT 0. wi_index = sy-index. ASSIGN COMPONENT wi_index OF STRUCTURE <fs_line> TO <fs_field_s>. IF sy-subrc EQ 0. DESCRIBE FIELD <fs_field_s> TYPE wf_ltype

OUTPUT-LENGTH wf_length.

IF wf_maximum LT wf_length. wf_maximum = wf_length. ENDIF. ENDIF. wi_count = wi_count - 1. ENDWHILE. CLEAR:wa_comp,i_comp[]. wa_comp-name = 'FIELD'(005). wa_comp-type ?= cl_abap_elemdescr=>get_c( wf_maximum ). APPEND wa_comp TO i_comp. TRY. wf_type_struct = cl_abap_structdescr=>create( p_components = i_comp ). CATCH cx_sy_struct_creation INTO wf_cx. PERFORM exp_txt USING wf_cx. MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4 RAISING dynamic_table_generation. ENDTRY. TRY. wf_type_table = cl_abap_tabledescr=>create( wf_type_struct ). CATCH cx_sy_table_creation INTO wf_cx. PERFORM exp_txt USING wf_cx. MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4 RAISING dynamic_table_generation. ENDTRY. CREATE DATA: wf_data_tab TYPE HANDLE wf_type_table, wf_data_str TYPE HANDLE wf_type_struct. ASSIGN: wf_data_tab->* TO <fs_table>,

wf_data_str->* TO <fs_wa>. CLEAR wf_string. IF im_operation = c_upload. DO. READ DATASET im_path INTO wf_string. IF sy-subrc NE 0. IF sy-index EQ 1. MESSAGE e904 WITH 'No data in the file'(003) RAISING file_operation. ELSE. CLOSE DATASET im_path. EXIT. ENDIF. ENDIF. CLEAR <fs_table>[]. SPLIT wf_string AT im_separator INTO TABLE <fs_table>. LOOP AT <fs_table> ASSIGNING <fs_wa>. wi_index = sy-tabix. ASSIGN COMPONENT 'FIELD' OF STRUCTURE <fs_wa> TO <fs_field_s>. IF sy-subrc = 0. ASSIGN COMPONENT wi_index OF STRUCTURE <fs_line> TO <fs_field_t>. IF sy-subrc = 0. TRY. <fs_field_t> = <fs_field_s>. CATCH cx_sy_conversion_no_number. ENDTRY. ENDIF. ENDIF. ENDLOOP.

APPEND <fs_line> TO it_data. CLEAR <fs_line>. ENDDO. ELSE. LOOP AT it_data ASSIGNING <fs_line>. wi_index = 0. DO. wi_index = sy-index. ASSIGN COMPONENT wi_index OF STRUCTURE <fs_line> TO <fs_field_s>. IF sy-subrc = 0. ASSIGN COMPONENT 'FIELD' OF STRUCTURE <fs_wa> TO <fs_field_t>. TRY. <fs_field_t> = <fs_field_s>. CATCH cx_sy_conversion_no_number. ENDTRY. CONDENSE <fs_field_t>. IF wi_index = 1. wf_string = <fs_field_t>. ELSE. CONCATENATE wf_string <fs_field_t> INTO wf_string SEPARATED BY im_separator. ENDIF. ENDIF. IF wi_index = wf_total_fields.

CONDENSE wf_string. TRANSFER wf_string TO im_path. CLEAR wf_string. EXIT. ENDIF.

ENDDO. ENDLOOP. CLOSE DATASET im_path. ENDIF. ENDFUNCTION.

Subroutines
Subroutines

*&---------------------------------------------------------------------* *& Form CHECK_AUTHORITY

*&---------------------------------------------------------------------* * text

*----------------------------------------------------------------------* * * -->P_C_ACTIVITY_READ -->P_WF_FILENAME text text

*----------------------------------------------------------------------* FORM check_authority USING pu_activity TYPE any pu_filename TYPE fileextern. CALL FUNCTION 'AUTHORITY_CHECK_DATASET' EXPORTING activity filename EXCEPTIONS error_message = 1 OTHERS ENDFORM. = 2. " CHECK_AUTHORITY = pu_activity = pu_filename

*&---------------------------------------------------------------------*

*&

Form

EXP_TXT

*&---------------------------------------------------------------------* * text

*----------------------------------------------------------------------* * -->P_WF_OBJ text

*----------------------------------------------------------------------* FORM exp_txt USING pu_obj TYPE any.

CALL FUNCTION 'RSAN_MDL_EXCEPTION_TO_SYMSG' EXPORTING ir_exception = pu_obj i_msgty = 'E'.

ENDFORM.

" EXP_TXT

Testing functionality
Demo Program REPORT yupload_download.

INCLUDE zmm3x006. DATA:i_mard TYPE TABLE OF mard. DATA:wf_alv TYPE REF TO cl_salv_table. SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001. PARAMETER : p_cpage TYPE zcacodep-cpcodepage OBLIGATORY DEFAULT '4110'. SELECTION-SCREEN END OF BLOCK b1. PARAMETERS dir1 TYPE localfile . PARAMETERS dir2 TYPE localfile . AT SELECTION-SCREEN ON p_cpage. PERFORM validate_cpage USING p_cpage. START-OF-SELECTION. IF p_dir IS NOT INITIAL. SELECT * FROM mard INTO TABLE i_mard UP TO 100 ROWS.

CALL FUNCTION 'Z_FILE_UPLOAD_DOWNLOAD' EXPORTING im_path im_operation im_codepage im_separator * * IM_APPEND IM_AUTHORITY_CHECK TABLES it_data EXCEPTIONS error_found file_operation dynamic_table_generation OTHERS IF sy-subrc <> 0. MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. ENDIF. ELSE. CLEAR i_mard[]. CALL FUNCTION 'Z_FILE_UPLOAD_DOWNLOAD' EXPORTING im_path im_operation im_codepage im_separator * * IM_APPEND IM_AUTHORITY_CHECK = dir2 = 'U' = p_cpage = cl_abap_char_utilities=>horizontal_tab = = "Upload = 1 = 2 = 3 = 4. = i_mard = dir1 = 'D' = p_cpage = cl_abap_char_utilities=>horizontal_tab = = "Download

TABLES it_data EXCEPTIONS error_found file_operation dynamic_table_generation OTHERS IF sy-subrc <> 0. MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. ENDIF. TRY. cl_salv_table=>factory( IMPORTING r_salv_table = wf_alv CHANGING CATCH cx_salv_msg. ENDTRY. wf_alv->display( ). t_table = i_mard ). "#EC NO_HANDLER = 1 = 2 = 3 = 4. = i_mard

ENDIF.

Labels parameters

Labels

S-ar putea să vă placă și