sapdev logo background
sapdev logo sapdev logo
Comments

Uplaod file within SAP BSP application using HTML and ABAP coding




Uploading a file into a SAP BSP application is actually straight forward and if you follow the steps below you can have it up and running in a few minutes.

Step 1 - Create new BSP Application
Using SE80 create BSP Application (I.e. ZTESTBSP).

Step 2 - Create new page (index.htm)
Right click on the BSP object name and select create->page (with flow logic).


Step 3 - Populate 'Layout' tab
Add the following code into the layout section of the new page (index.htm). Save and activate the page and bsp object.

<%@page language="abap" %>
<%@extension name="htmlb" prefix="htmlb" %>

<form method="POST" enctype="multipart/form-data">
   Please enter your file name by using the browse button:
   <br><input size="40" type="file" name="filename">
   <br><input type=submit name="onInputProcessing(upload)" value="Upload">
</form>

Step 4 - Populate 'OnInputProcessing'
Within the 'Event Handler' tab add the following code to the OnInputProcessing event.

* event handler for checking and processing user input and
* for defining navigation

* data declarations
data: ld_value           type string,
      ld_entity          type ref to if_http_entity,
      ld_data            type xstring,
      ld_datatmp         type xstring,
      ld_filename        type string,
      ld_type            type string,
      ld_length          type string,
      ld_lengthtmp       type string,
      ld_numparts        type i,
      wa_binary_content  type SDOKCNTBIN,
      it_binary_content  type O2_DATA_TABLE.
data: ld_datastring      type string.
data: it_stringtab       type standard table of string.
data: ld_contsize        type i.

types: begin of t_char,
   text(1022) type c,
  end of t_char.
data: it_chartab         type STANDARD TABLE OF t_char.

*Constants for file processing
constants: con_tab  type c value cl_abap_char_utilities=>HORIZONTAL_TAB,
           new_line type c value cl_abap_char_utilities=>NEWLINE,
           cf type c value cl_abap_char_utilities=>CR_LF.

*User action value
case event_id.
  when 'upload'.
* find number of multiparts of file
    ld_numparts  = request->num_multiparts( ).
    do ld_numparts  times.
      ld_entity = request->get_multipart( sy-index ).
      ld_value  = ld_entity->get_header_field( '~content_filename' ).

      if not ld_value is initial.
        ld_filename = ld_value.
        ld_type = ld_entity->get_header_field( 'Content-Type' ).
*       Get file content
        ld_data = ld_entity->get_data( ).

*       Get size of file
        ld_length = xstrlen( ld_data ).

*       Move BINARY data into itab 1022 characters at a time
        ld_datatmp = ld_data.
        ld_lengthtmp = ld_length.
        while ld_lengthtmp > 1022.
          wa_binary_content-line = ld_datatmp(1022).
          append wa_binary_content to it_binary_content.
          shift ld_datatmp by 1022 places in byte mode.
          ld_lengthtmp = xstrlen( ld_datatmp ).
        endwhile.

*       Move remainder of file into table
        wa_binary_content-line = ld_datatmp.
        append wa_binary_content to it_binary_content.

*       Convert binary data table into char table, retains line breaks and
*       put each line of file into new table row
        ld_contsize = ld_lengthtmp.
        CALL FUNCTION 'SCMS_BINARY_TO_TEXT'
          EXPORTING
            INPUT_LENGTH          = ld_contsize
*            FIRST_LINE            = 0
*            LAST_LINE             = 0
            APPEND_TO_TABLE       = 'X'
*            MIMETYPE              = ' '
*            WRAP_LINES            = ' '
*            ENCODING              =
*         IMPORTING
*           OUTPUT_LENGTH         =
          TABLES
            BINARY_TAB            = it_binary_content
            TEXT_TAB              = it_chartab
          EXCEPTIONS
            FAILED                = 1
            OTHERS                = 2.

*       Convert binary data table into string field
        CALL FUNCTION 'SCMS_BINARY_TO_STRING'
          EXPORTING
            INPUT_LENGTH  = ld_contsize
*           FIRST_LINE    = 0
*           LAST_LINE     = 0
*           MIMETYPE      = ' '
*           ENCODING      =
          IMPORTING
            TEXT_BUFFER   = ld_datastring
*           OUTPUT_LENGTH =
          TABLES
            BINARY_TAB    = it_binary_content
          EXCEPTIONS
            FAILED        = 1
            OTHERS        = 2.

*       Split string value at line breaks into rows of itab
        split ld_datastring AT new_line INTO TABLE it_stringtab.
      endif.
    enddo.
endcase.  

Step 5 - Activate and test
Save everything and press the activate button, if all is ok return the the index.htm page and press the test button, this should open up a browser window and display your newly created BSP.


Click the browse button and choose your filename from the standard windows file browser functionality and then press upload.


Step 6 - What should happen??
Well actually nothing visually! To make this example as simple as possible to implement, when you click the upload button nothing should happen on screen, but if you place an external breakpoint at the end of the code and then press the upload button you should see that the data from your file is contained within the following variables in different formats:


ld_datastring - File data stored as a single string value including newline values


it_chartab - File data stored as a char table with each line of data appearing in a new table row (limited row length)


it_datastring - File data stored as a string table with each line of data appearing in a new table row (allows for larger strings of data per row)


See here for more information and examples of BSP development




comments powered by Disqus