Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Convert xstring to string (File-Upload)

wolfgang_brunneder
Participant
0 Likes
2,346

Hello!

I've created a BSP-page to upload a file (types PDF, jpg, doc) and save it with a BO using GOS. Unfortunately I face a problem converting file-content xstring to string using class CL_ABAP_CONV_IN_CE. First I tried method CREATE with default-encoding, but in this case calling method READ raises exception CX_SY_CONVERSION_CODEPAGE. So I tried the following code: There is no exception, but I'm not able to open the saved document in the GOS of the BO. I assume that I'm using a wrong codepage.

CALL FUNCTION 'NLS_GET_FRONTEND_CP'

EXPORTING

langu = sy-langu

  • FETYPE = 'MS'

IMPORTING

frontend_codepage = lv_codepage

EXCEPTIONS

illegal_syst_codepage = 1

no_frontend_cp_found = 2

internal_or_db_error = 3

OTHERS = 4.

TRY.

lv_encoding = lv_codepage.

cl_abap_conv_in_ce=>create(

EXPORTING

encoding = lv_encoding

input = lv_file " Type XSTRING

RECEIVING

conv = conv

).

conv->read(

IMPORTING

data = file_content " Type STRING

).

CATCH cx_parameter_invalid_range .

CATCH cx_sy_codepage_converter_init .

CATCH cx_sy_conversion_codepage .

CATCH cx_parameter_invalid_type .

ENDTRY.

I'm looking forward to your hints!

Thanks in advance!

Greetings

Wolfgang

8 REPLIES 8
Read only

Former Member
0 Likes
1,329

hi,

Check this code..


DATA       convin        TYPE REF TO cl_abap_conv_in_ce,

CALL METHOD cl_abap_conv_in_ce=>create
    EXPORTING
      encoding    = 'UTF-8'
      endian      = 'L'
      ignore_cerr = 'X'
      replacement = '#'
      input       = l_buffer              " Xstring
    RECEIVING
      conv        = convin.

  CALL METHOD convin->read
    IMPORTING
      data = l_data.                  " String

Edited by: Avinash Kodarapu on Mar 11, 2009 4:04 PM

Read only

0 Likes
1,329

Thanks for your promp reply!

Unfortunately your hint does not solve the problem. If I open the saved file in the BO the pdf viewer (acrobat) reports that the file my be damaged!

Debugging my code I found out that the generated string contains quite a lot # signs. I assume that many characters could not be converted and where replaced by #.

Greetings

Wolfgang

Addition: We are running a unicode-system!

Edited by: Wolfgang Brunneder on Mar 11, 2009 1:52 PM

Read only

0 Likes
1,329

Hi,

For this kind of convertion use FM HR_KR_XSTRING_TO_STRING . You will get rid of unwanted # marks when uploading the file.

Regards

Marcin

Read only

0 Likes
1,329

Hello Marcin!

Thanks for your reply! Can you tell me how I can find the correct codepage to pass to the function module?

Thanks!

Greetings Wolfgang

Read only

0 Likes
1,329

Hi Wolfgang,

You don't use any code page here. Just give your XSTRING in input and receive STRING on output. As you read the file byte by byte you don't need to worry about text convertion, as it is read excatly as it appears.

Regards

Marcin

Read only

0 Likes
1,329

Hi Marcin!

I've tried your hint, but it doesn't work. But now I found a working solution:

cl_abap_conv_in_ce=>create(

EXPORTING

encoding = 'NON-UNICODE'

endian = 'L'

RECEIVING

conv = conv

).

conv->convert(

EXPORTING

input = fileupload->file_content " type xstring

IMPORTING

data = file_content " type string

).

Thanks for your help! I rewarded some points!

Greetings Wolfgang

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
1,329

Hello,

May be the codepage retrieval is not correct. You can use this method to get the codepage & use your code:

DATA:
rc TYPE i  VALUE 0,
v_encoding TYPE abap_encoding.

* Get the LOGON Encoding for the particular user
CALL METHOD cl_gui_frontend_services=>get_saplogon_encoding
  CHANGING
    rc                            = rc
    file_encoding                 = v_encoding
  EXCEPTIONS
    cntl_error                    = 1
    error_no_gui                  = 2
    not_supported_by_gui          = 3
    cannot_initialize_globalstate = 4
    OTHERS                        = 5.
IF sy-subrc <> 0 OR
   v_encoding = 0.
  CLEAR v_encoding.
ENDIF.

Hope this helps.

BR,

Suhas

Read only

0 Likes
1,329

Thanks for your reply!

Unfortunately cl_gui_frontend_services=>get_saplogon_encoding doesn't work (sy-subrc = 3) - I assume because I'm working with BSP.