cancel
Showing results for 
Search instead for 
Did you mean: 

Uploading the contents of file into custom table

Former Member
0 Kudos

Hi ,

I have a req where in i want to put the contents of the file into the Z table . I have taken a file upload UI element and in that i have taken 4 attributes as file name f, file type ,file size and file contents and binded this file contents to the data property of the UI element. Now with out explicitly defining the fields i want to put all this content of file into the Z table created for any file created or if i could put the whole text file into that table so that any one could see that file even though its not saved in the desktop for that user.

Can any one help me out?

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi,

Once you bind the file content attribute of the upload file component with an attribute in context of type XString, whenever you press the browse button and select a file, the entire file will be uploaded to that context attribute. You can save it on the database table directly. But while storing the file content in database use RAWSTRING as the type for the file content field. You can store any type of file.

Regards,

Fareez

Former Member
0 Kudos

Hi Fareez,

Am doing the same thing which u told but still the contents are getting stored in the binary format in the input field of the table.

pleasse find the code to insert it nto the table.

ls_file-file_contents = ls_upload-file_contents.

insert ZFILE_UPLOAD FROM ls_file.

but the o/p which am getting in the table is like. 57213674563256252567884245633557632243658534457

Can you please help me out

Former Member
0 Kudos

Hi,

First you need to convert the file contents from xstring to string by using FM HR_KR_XSTRING_TO STRING.

are you trying to store the data inside the file or directly the file?

Former Member
0 Kudos

Yes i am converting it inot the lstring using the same FM which you have given. I have to store the total file but the only doubt is like if its a input field how could i save a file form. Can you please check the code i want to save the total file into the table.

DATA lo_nd_upload TYPE REF TO if_wd_context_node.

DATA lo_el_upload TYPE REF TO if_wd_context_element.

data i_data TYPE STANDARD TABLE OF string.

DATA ls_upload TYPE wd_this->Element_upload.

data lv_field type string.

data lt_file type TABLE OF ZFILE_UPLOAD.

data ls_file LIKE LINE OF lt_file.

data l_xstring type xstring.

data fields TYPE string_table.

data l_String type string.

  • navigate from <CONTEXT> to <UPLOAD> via lead selection

lo_nd_upload = wd_context->get_child_node( name = wd_this->wdctx_upload ).

  • @TODO handle non existant child

  • IF lo_nd_upload IS INITIAL.

  • ENDIF.

  • get element via lead selection

lo_el_upload = lo_nd_upload->get_element( ).

  • @TODO handle not set lead selection

IF lo_el_upload IS INITIAL.

ENDIF.

  • get all declared attributes

lo_el_upload->get_static_attributes(

IMPORTING

static_attributes = ls_upload ).

DATA lv_file_contents TYPE wd_this->Element_upload-file_contents.

  • navigate from <CONTEXT> to <UPLOAD> via lead selection

  • @TODO handle non existant child

  • IF lo_nd_upload IS INITIAL.

  • ENDIF.

  • get element via lead selection

lo_el_upload = lo_nd_upload->get_element( ).

  • alternative access via index

  • lo_el_upload = lo_nd_upload->get_element( index = 1 ).

  • @TODO handle not set lead selection

IF lo_el_upload IS INITIAL.

ENDIF.

  • get single attribute

lo_el_upload->get_attribute(

EXPORTING

name = 'FILE_CONTENTS'

IMPORTING

value = l_xstring ).

  • for text file conversion we have to FM

CALL FUNCTION 'HR_KR_XSTRING_TO_STRING'

EXPORTING

  • FROM_CODEPAGE = '8500'

IN_XSTRING = l_xstring

  • OUT_LEN =

IMPORTING

OUT_STRING = l_string.

.

SPLIT l_string AT cl_abap_char_utilities=>newline INTO TABLE i_data.

LOOP AT i_data INTO l_string.

SPLIT l_string AT cl_abap_char_utilities=>horizontal_tab INTO TABLE fields.

read TABLE fields into lv_field index 1.

endloop.

  • ls_upload will contain all the file details

ls_file-file_size = xstrlen( ls_upload-file_contents ) * lv_float .

ls_file-file_name = ls_upload-file_name.

ls_file-file_type = ls_upload-file_type.

ls_file-file_contents = ls_upload-file_contents.

insert ZFILE_UPLOAD FROM ls_file.

thanks

Former Member
0 Kudos

Hi,

If you want to upload the whole file, then the file contents must be of type RAWSTRING and you

dont need t convert it. you should directly store it in the table and it would be stored same as a

RAWSTRING in table also. So you should not convert or do any kind of processing to the file.

Follow this link :

http://wiki.sdn.sap.com/wiki/display/WDABAP/UploadandDownloadfilesinWebdynproABAP

Former Member
0 Kudos

Hi Harshith,

Thanks for providing the link . But still after doing it and checking it in the field contents in the table i am getting the binary format result . How to overcome that. Wants that the o/p which is like this when we click on download the same way should be done in table also? or If not possible then how could i see the proper data.

Thanks

former_member199125
Active Contributor
0 Kudos

Its possible.

Once you got the uploaded data into ztable (make sure you store the file content in string format ).

Dispaly the ztable records in a table with last columns (cell editore link to action ). in that action, convert the corresponding string record into internal table.

Once you store the internal table download the file using attach_file_to_response method..

Regards

Srinivas

Former Member
0 Kudos

Hi swarn,

You don't need to convert to store it into the table. Just define that type as RAWSTRING and context type as XSTRING. To check whether the exact file is stored, just use file download element i.e. Download it and check. It should work.

Regards

Fareez

Former Member
0 Kudos

Hi swarn,

You don't need to convert to store it into the table. Just define that type as RAWSTRING and context type as XSTRING. To check whether the exact file is stored, just use file download element i.e. Download it and check. It should work.

Regards

Fareez

Answers (2)

Answers (2)

Former Member
0 Kudos

Hi,

It wont be like that the file will be stored in sap server after u upload.So from any system we can see it.

Karthik.R

Former Member
0 Kudos

Hi,

Did u mean any type of file contents can be upload or particular format like pdf ,excel file type..?

Thanks & Regards,

Jana

Former Member
0 Kudos

Hi ,

Any type of file contents other than excel but i dont want to explicitly define the fields which i need it to be inserted into the table i needall the fields whatever is der in that file. Is there any logic where i could do this . I have binded this attribute field contents to the data source property.