Skip to Content
author's profile photo Former Member
Former Member

File Download / Upload Question

Hi All,

In my BSP application, I am downloading the contents of internal table to a CSV file format. For this, i initially convert the data line to XSTRING using 'SCMS_STRING_TO_XSTRING' function module with the mimetype being 'APPLICATION/MSEXCEL; charset=utf-16le'. As can be seen in the below code, i then use the standard code to write the contents as an attachment. Please note that at this point, the file is stored as a CSV file, but the file type seems to be unicode text. I make some changes to the file and save it. And, i try to load the same file, i am getting a file open error. However, when i open the file in Excel and save as type 'CSV', i am able to successfully load it. How do i change so that the BSP save as File type "Comma Separated". Any help is greatly appreciated. Thanks,Raj

Below is the code:

Download File -

app_type = 'APPLICATION/MSEXCEL; charset=utf-16le'.

call function 'SCMS_STRING_TO_XSTRING'

exporting

text = lv_line_item

mimetype = app_type

importing

buffer = l_xstring

exceptions

failed = 1

others = 2.

mresponse->delete_header_field( name =

if_http_header_fields=>cache_control ).

mresponse->delete_header_field( name = if_http_header_fields=>expires

).

mresponse->delete_header_field( name = if_http_header_fields=>pragma

).

  • start Excel viewer either in the Browser or as a separate window

mresponse->set_header_field( name = 'content-disposition'

value = 'attachment; filename = test.csv' ).

data l_len type i.

l_len = xstrlen( lt_xstring ).

mresponse->set_data( data = lt_xstring

length = l_len ).

navigation->response_complete( ).

Upload File -

lcl_data ?= cl_htmlb_manager=>get_data(

request = runtime->server->request

name = 'fileUpload'

id = 'SalesFile').

lv_str =

cl_abap_conv_in_ce=>create( input = lcl_data->file_content ).

lv_str->read( importing data = v_filedata ).

Add comment
10|10000 characters needed characters exceeded

3 Answers

  • Posted on Aug 01, 2006 at 07:36 PM

    The problem is probably because you didn't add a BOM (Byte Order Mark) to the beginning of the file. The BOM tells the accepting operating system what Endian you used in your Unicode encoding. You are specifying UTF-16le. The le stands for Little Endian. Try adding some code like the following after the conversion to the binary string:

    ****Add the UTF-16 Little Endian Byte Order Mark to the begining of the file
      concatenate  cl_abap_char_utilities=>byte_order_mark_little
                   r_xstring
                   into r_xstring in byte mode.

    Add comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Aug 01, 2006 at 07:55 PM

    Thanks Thomas for your reply. I forgot to add a piece to my initial question. In the download section, after the call function 'SCMS_STRING_TO_XSTRING', I am doing the following process:

    • Add the Byte Order Mark - UTF-16 Little Endian

    concatenate cl_abap_char_utilities=>byte_order_mark_little

    lt_xstring

    into lt_xstring in byte mode.

    mresponse->set_header_field( name = 'content-type'

    value = 'APPLICATION/MSEXCEL; charset= utf-16le' ).

    So, I am already doing what you suggested me. But still, the file type is "Unicode text (.txt)". How do I make this "Comma separated"?. Thanks again.

    Add comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Aug 02, 2006 at 01:51 PM

    Hi All,

    Can someone give me suggestions as to how to fix this issue? I am missing a small thing which i can't figure out. Thanks again for your help.

    Raj

    Add comment
    10|10000 characters needed characters exceeded

    • I think you should prepare the data before calling SCMS_STRING_TO_XSTRING. It does not take care of CSV, DAT anad so on.

      You should use CONCATENATE statement looping on your reecords and columns...

      Did you had a look to the blog "Downloading data into Excel with Format Options (from SAP Web Applications)" /people/sergio.ferrari2/blog/2006/06/11/downloading-data-into-excel-with-format-options-from-sap-web-applications ?

      After reading the blog answer me: "Do you really need a csv file?"