Monday, September 15, 2014

ABAP TIP : FILE management with CBO program

ABAP TIP : FILE management with CBO program

Let’s make a program that can upload and download file.
User can select file and upload.






Generally, we use the ‘ASC’ mode when calling function ‘GUI_UPLOAD/GUI_DOWNLOAD’.

 CALL FUNCTION 'GUI_UPLOAD'
    
EXPORTING
      filename                
gv_fname
      filetype                'ASC'


This program aims to upload/donwload file, so we must use the ‘BIN’ mode when calling function.
If we save the file as a BIN mode, it’s possible to download as a origin file.

 CALL FUNCTION 'GUI_UPLOAD'
    
EXPORTING
      filename                
gv_fname
      filetype                'BIN'

Fisrt of all, let’s create 2 table for saving header and contents of file.







And, Let’s write program and activate.

REPORT  zfile.

 
TABLES : zfilehead, zfiledata, sscrfields.
 
DATA : gs_fhead  LIKE zfilehead,
        gt_fdata  
LIKE zfiledata OCCURS WITH HEADER LINE.

*attache file
DATA : BEGIN OF gt_uptab OCCURS 0,
         content
(255TYPE x,
       
END OF gt_uptab.

DATA :  gv_length  TYPE int4,
        gv_fname   
TYPE string,
        gv_fname2  
TYPE string,
        gv_ftype   
TYPE zfilehead-filetype,
        gv_temp
(120).


DATA:   gv_work_dir TYPE string,
        gv_cmd
(128).

PARAMETERS : p_carrid TYPE s_carr_id OBLIGATORY,
             p_fname 
TYPE zfilehead-filename.
SELECTION-SCREEN FUNCTION KEY 1.

INITIALIZATION.
    sscrfields
-functxt_01 'Display file'.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_fname.

DATA:   l_rc                TYPE i VALUE 1,
        l_window_title      
TYPE string,
        lt_file_table       
TYPE filetable,
        ls_file             
TYPE file_table.

CALL METHOD cl_gui_frontend_services=>file_open_dialog
    
EXPORTING
      window_title            
'Select file'
      file_filter             
'*.*'
      multiselection          
space
    
CHANGING
      file_table              
lt_file_table
      rc                      
l_rc.
      
IF sy-subrc EQ 0.
      
READ TABLE lt_file_table INDEX INTO ls_file.
      p_fname 
ls_file-filename.
      
ENDIF.

 
CLEAR: gt_uptab, gt_uptab[].
 gv_fname 
p_fname.
 
CALL FUNCTION 'GUI_UPLOAD'
    
EXPORTING
      filename                
gv_fname
      filetype                
'BIN'
    
IMPORTING
      filelength              
gv_length
    
TABLES
      data_tab                
gt_uptab.

START-OF-SELECTION.

*& file name
  
CLEAR gv_temp.
  
DO.
    
SPLIT gv_fname AT '\' INTO gv_temp gv_fname.
    
IF gv_fname EQ space.
      gv_fname 
gv_temp.
      
EXIT.
    
ENDIF.
    
IF sy-index 100. EXIT. ENDIF.
  
ENDDO.

*& file type
  
CLEAR gv_temp.
  gv_fname2 
gv_fname.
  
DO.
    
SPLIT gv_fname2 AT '.' INTO gv_temp gv_fname2.
    
IF gv_fname2 EQ space.
      gv_fname2 
gv_temp.
      
EXIT.
    
ENDIF.
    
IF sy-index 100. EXIT. ENDIF.
  
ENDDO.

  gv_ftype 
gv_fname2+0(3).
  
TRANSLATE gv_ftype TO UPPER CASE.
*& Header
  gs_fhead
-carrid   p_carrid.
  gs_fhead
-filename gv_fname.
  gs_fhead
-filetype gv_ftype.
  gs_fhead
-length   gv_length.
  
MODIFY zfilehead FROM gs_fhead.

*& Data.
  
LOOP AT gt_uptab.
    gt_fdata
-carrid p_carrid.
    gt_fdata
-content gt_uptab-content.
    gt_fdata
-seq sy-tabix.
    
APPEND gt_fdata.
  
ENDLOOP.
  
MODIFY zfiledata FROM TABLE gt_fdata.
  
IF sy-subrc EQ 0.
     
MESSAGE 'SUCCESS' TYPE 'S'.
  
ELSE.
     
MESSAGE 'ERROR' TYPE 'E'.
  
ENDIF.


  
AT SELECTION-SCREEN.
    
CHECK sscrfields-ucomm 'FC01'.
CLEAR : gs_fhead, gt_fdata, gt_fdata[].

*& Header
  
SELECT SINGLE INTO gs_fhead FROM zfilehead
                 
WHERE carrid p_carrid.
  
IF sy-subrc NE 0.
         
MESSAGE 'There is no file' TYPE 'E'.
  
ENDIF.

*& detail
  
SELECT INTO CORRESPONDING FIELDS OF TABLE gt_fdata
           
FROM zfiledata
           
WHERE carrid p_carrid.

  
CLEAR: gt_uptab, gt_uptab[].

  
SORT gt_fdata BY seq.
  
LOOP AT gt_fdata.
    gt_uptab
-content gt_fdata-content.
    
APPEND gt_uptab.
  
ENDLOOP.

* Get SAP Working Directory.
  
CALL METHOD cl_gui_frontend_services=>get_sapgui_workdir
    
CHANGING
      sapworkdir 
gv_work_dir.

*& file name
  
CONCATENATE gv_work_dir '\' gs_fhead-filename INTO gv_fname.

*& Download
  
CALL FUNCTION 'GUI_DOWNLOAD'
    
EXPORTING
      bin_filesize            
gs_fhead-length
      filename                
gv_fname
      filetype                
'BIN'
    
TABLES
      data_tab                
gt_uptab.

*& file run
  gv_cmd 
gv_fname.
  
CALL FUNCTION 'GUI_RUN'
    
EXPORTING
      command          
gv_cmd.

Select test image file and execute.



After uploading, press the button ‘display file’ and you can see the file uploaded.




No comments:

Post a Comment