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
Function Module Importing parameters
Parameter | Typing | Associated type | Description | Optional |
IM_PATH | TYPE | LOCALFILE | File path for Upload/Download | No |
IM_OPERATION | TYPE | ABAP_CHAR1 | File Operation Upload/Download | No |
IM_CODEPAGE | TYPE | CPCODEPAGE | SAP Character Set ID | Yes |
IM_SEPARATOR | TYPE | ABAP_CHAR1 | Field Separator | No |
IM_APPEND | TYPE | ABAP_CHAR1 | Dataset append mode | Yes |
IM_AUTHORITY_CHECK | TYPE | ABAP_CHAR1 | Authority check | Yes |
Tables parameters
Parameter | Typing | Associated type | Description |
IT_DATA |
|
| Table with data |
Exceptions
Exception | Short text |
INVALID_INPUT | Invalid Inputs |
FILE_OPERATION | File Operation |
DYNAMIC_TABLE_GENERATION | Dynamic table Creation |
Top Include
Error rendering macro 'code': Invalid value specified for parameter 'com.atlassian.confluence.ext.code.render.InvalidValueException'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 TYPE REF TO DATA, WF_DATA_STR TYPE REF TO DATA. DATA:WA_COMP LIKE LINE OF I_COMP, WA_FIELDS TYPE ABAP_COMPDESCR. FIELD-SYMBOLS: <FS_LINE> TYPE ANY, <FS_FIELD_S> TYPE ANY, <FS_FIELD_T> TYPE ANY, <FS_TABLE> TYPE STANDARD TABLE, <FS_WA> TYPE ANY.
Source Code
Error rendering macro 'code': Invalid value specified for parameter 'com.atlassian.confluence.ext.code.render.InvalidValueException'FUNCTION Z_FILE_UPLOAD_DOWNLOAD. *"---------------------------------------------------------------------- *"*"Local Interface: *" IMPORTING *" VALUE(IM_PATH) TYPE LOCALFILE *" VALUE(IM_OPERATION) TYPE ABAP_CHAR1 *" VALUE(IM_CODEPAGE) TYPE CPCODEPAGE *" VALUE(IM_SEPARATOR) TYPE ABAP_CHAR1 *" VALUE(IM_APPEND) TYPE ABAP_CHAR1 OPTIONAL *" VALUE(IM_AUTHORITY_CHECK) TYPE ABAP_CHAR1 OPTIONAL *" 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 FOR INPUT IN TEXT MODE ENCODING DEFAULT IGNORING CONVERSION ERRORS. 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
Error rendering macro 'code': Invalid value specified for parameter 'com.atlassian.confluence.ext.code.render.InvalidValueException'* SUBROUTINES *&---------------------------------------------------------------------* *& Form CHECK_AUTHORITY *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * -->P_C_ACTIVITY_READ text * -->P_WF_FILENAME text *----------------------------------------------------------------------* FORM CHECK_AUTHORITY USING PU_ACTIVITY TYPE ANY PU_FILENAME TYPE FILEEXTERN. CALL FUNCTION 'AUTHORITY_CHECK_DATASET' EXPORTING ACTIVITY = PU_ACTIVITY FILENAME = PU_FILENAME EXCEPTIONS ERROR_MESSAGE = 1 OTHERS = 2. ENDFORM. " CHECK_AUTHORITY *&---------------------------------------------------------------------* *& 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
Error rendering macro 'code': Invalid value specified for parameter 'com.atlassian.confluence.ext.code.render.InvalidValueException'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 = DIR1 IM_OPERATION = 'D' "Download IM_CODEPAGE = P_CPAGE IM_SEPARATOR = CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB * IM_APPEND = * IM_AUTHORITY_CHECK = TABLES IT_DATA = I_MARD EXCEPTIONS ERROR_FOUND = 1 FILE_OPERATION = 2 DYNAMIC_TABLE_GENERATION = 3 OTHERS = 4. 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 = DIR2 IM_OPERATION = 'U' "Upload IM_CODEPAGE = P_CPAGE IM_SEPARATOR = CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB * IM_APPEND = * IM_AUTHORITY_CHECK = TABLES IT_DATA = I_MARD EXCEPTIONS ERROR_FOUND = 1 FILE_OPERATION = 2 DYNAMIC_TABLE_GENERATION = 3 OTHERS = 4. 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 T_TABLE = I_MARD ). CATCH CX_SALV_MSG. "#EC NO_HANDLER ENDTRY. WF_ALV->DISPLAY( ). ENDIF.
No comments:
Post a Comment
Note: only a member of this blog may post a comment.