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


Related Articles

Javascript to capture when a user closes or leaves sap bsp html page
Using AJAX functionality within our SAP BSP
Close BSP Session (type 'Pluggin HTTP') including when in SAP portal
SAP BSP to auto select region when user selects country using HTML and Javascript
HTML and Javascript code to force enter key to perform tab
JavaScrip command getElementById useful examples
MVC BSP input field - Demonstrate how to retrieve a value entered into text input field
Allowing multiple instances of a BSP application to be run by a user
Execute Standard SAP Transaction from BSP using dynamically created shortcut
UUseful SAP BSP application code - ABAP, HTML, HTMLB, Javascript, CSS
Creating a BSP Application class to help store and pass data betwenn your BSP pages
Training Course and Workshop Booking Form
BSP checkboxes using HTML and the OnInputProcessing BSP Event
BSP checkboxes using HTML and the OnInputProcessing BSP Event
Check HTML checkbox is checked using Javascript within your SAP BSP
BSP Dropdown List - create a BSP dropdown list which allows user selection!!
ABAP BSP to allow user to select from SAP HR org structure - CSS file
Java script to display tree structure on your SAP BSP web page
Create a Simple BSP - Simple BSP to display text and call section BSP page using HTML
SAP BSP Training course booking - Creating an example training course booking form using BSP
Creating an SAP BSP which executes an ABAP report to retrieve data for use within BSP application
org selection BSP - detials.htm page
Display page of the SAP BSP example application to store favourite tcodes as a cookie
Execute.htm page of the SAP BSP example application to store favourite tcodes as a cookie
Favourites cookie bsp - initial page to display input fields and retrieve user input
BSP using cookies - Demonstrate use of cookies within a BSP to store user favourits
ABAP Export data to memory - Demonstrate use of ABAP program to retrieve data for use in BSP
MVC BSP HTMLB input field - Demonstrate how to retrieve a value entered into text input field
BSP development using standard HTML code instead of Business HTML (HTMLB)
Business HTML (HTMLB) - List of business HTML tags you can use within your BSP
Adding JavaScript to BSP (HTML) pages - Shows how to add javascript to your BSP pages
Example JavaScript code which can be added to your BSP application
BSP MIME Objects for Org. search application
Call onInputProcessing event from SAP BSP page via href or button
Organisation selection BSP - close internet window using javascript
Organisation selection BSP - get server side cookie ( get_server_cookie )
orgaisation selection BSP - set and get server side cookie ( set_server_cookie )
Display orgaisation selection BSP
BSP which allows HR Organisation Structure search and selection using javascript
Organisation update program - get server side cookie ( get_server_cookie )
BSP to capture user entry into HTML input fields
Capture BSP radiobutton selection using HTML and the OnInputProcessing BSP Event
BSP checkboxes using HTML and the OnInputProcessing BSP Event
Stateful and stateless BSP applications
Get user entry into HTML input fields with SAP BSP
Javascript to capture when a user closes or leaves sap bsp html page
Creating a BSP using the Model View Controller ( MVC ) technique
Using SAP Model View Controller (MVC) development techniques to develop BSPs
Adding a JavaScript date selection field to a BSP
Creating a BSP using the Model View Controller ( MVC ) technique
Creating a BSP using the Model View Controller(MVC) technique
Creating a BSP using the Model View Controller ( MVC ) technique
Retrieve value from input text field within a model view controller ( MVC) BSP
Web Application Development - Example code and information on development using BSP
SAP BSP / Business Server Pages combine ABAP and other web technologies such as HTML, CSS
BSP development screen - developing application using business server pages
Standard Program created in SE38 or SE80