Application Development 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: 

How to calculate the size of binary file?

Former Member
0 Kudos

Hi!

I have to download a file in binary mode to presentation server but I need the file size in order to use the function.

Is there another function that calculates this size?

Thanks,

Cristian

1 ACCEPTED SOLUTION

ferry_lianto
Active Contributor
0 Kudos

Hi,

Please try this.


CALL METHOD l_gui_frontend_services=>file_get_size    
  EXPORTING
    file_name  = str   
  IMPORTING     
    file_size  = size.

CALL METHOD  cl_gui_cfw=>flush( ).

WRITE: / file(50), size.

Regards,

Ferry Lianto

11 REPLIES 11

ferry_lianto
Active Contributor
0 Kudos

Hi,

Please try this.


CALL METHOD l_gui_frontend_services=>file_get_size    
  EXPORTING
    file_name  = str   
  IMPORTING     
    file_size  = size.

CALL METHOD  cl_gui_cfw=>flush( ).

WRITE: / file(50), size.

Regards,

Ferry Lianto

Former Member
0 Kudos

Thanks for the reply but I did not explain the problem very well. Sorry for the mistake!

Actually, the program collects data from several tables and stores them into an internal table. Then, I have to download it to presentation server in binary mode.

So, I need the file size in order to use the download function. Is there a function that calculates this size?

Thanks again!

Cristian

0 Kudos

Why do you need the filesize to use the download function? File Size is not a required parameter.



call function 'GUI_DOWNLOAD'
  exporting
<b>*   BIN_FILESIZE                  =</b>
    filename                      = 'c:test.txt'
  tables
    data_tab                      = itab.


Regards,

Rich Heilman

0 Kudos

if the file type in BIN you need to specify the file size.

Regards

Raja

Put yourself on the SDN world map

(http://sdn.idizaai.be/sdn_world/sdn_world.html) and earn 25 points.

Spread the wor(l)d!

ferry_lianto
Active Contributor
0 Kudos

Hi,

I am not aware any FM is available.

But perhaps you can code something like this.


DATA: FILESIZE TYPE I.

FILESIZE = 0.

LOOP AT ITAB.
  FILESIZE = FILESIZE + STRLEN( ITAB ) + 2.
ENDLOOP.

WRITE: / 'File size is' , FILESIZE.

Hope this will help.

Regards,

Ferry Lianto

Clemenss
Active Contributor
0 Kudos

Hi Cristian,

it should be

describe table <your internal output table>.

size = sy-tfill * sy-tleng.

Regards,

Clemens

Former Member
0 Kudos

Hi Cristian,

I checked the FM <b>GUI_DOWNLOAD</b>.

For the BIN file option in the INCLUDE <b>LSFESF02</b>, checked the form <b>gui_bin_download</b>, in this form file size is calculated(size of whole table) is calculated if not specified.

Check this program(based in the logic in the form) will it satisfy your requirements.


REPORT ztest_1.

DATA : it_mara TYPE STANDARD TABLE OF mara WITH HEADER LINE.

DATA:   prc_lines              TYPE i VALUE 0,
        prc_line_len           TYPE i VALUE 0,
        prc_bin_filesize       TYPE i VALUE 0,
        size(10) .

FIELD-SYMBOLS: <f> TYPE ANY.




START-OF-SELECTION.


  SELECT * FROM mara INTO TABLE it_mara UP TO 500 ROWS.


  DESCRIBE TABLE it_mara LINES prc_lines.

  LOOP AT it_mara ASSIGNING <f>.
    DESCRIBE FIELD <f> LENGTH prc_line_len IN BYTE MODE.
    EXIT.
  ENDLOOP.

  prc_bin_filesize = prc_lines * prc_line_len.

  BREAK-POINT.

  size =   prc_bin_filesize.
  
  WRITE : / 'File Size : ' , size.

Regards,

AS

0 Kudos

>

> LOOP AT it_mara ASSIGNING <f>.

> DESCRIBE FIELD <f> LENGTH prc_line_len IN BYTE

> BYTE MODE.

> EXIT.

> ENDLOOP.

Here, we have release 4.6C so I cannot use in byte mode statement but your reply was very useful!

Thanks to everybody!

Regards,

Cristian

cuky
Participant
0 Kudos

I see it's been 10 years since the last reply, but I want to add my personal experience with this matter.

I'm writing a WD report that uploads a local file from my computer, and then saves it in the Documentum Archive. I get the file-data as an XSTRING object directly from the main-view's file-upload element. Then, I convert it (code below) into a table structured with TBL1024, which has one field of type TABL1024, which is of type RAW and has a length of, you guessed it, 1024 characters.

I've been experimenting with some of the code suggested here, and found that the best way to get the correct file-size is like this:

lv_file_size = XSTRLEN( iv_file_data ).

This gives me the actual size, in bytes, of the file. I double-checked it by right-clicking my test file and going to its properties (property 'Size' is relevant. 'Size on disk' is not, since we are talking about multiplications of 1024-characters and not 1000).

Here are my experimentation results.

Test file is a JPG image file.

Test file size: 68.5 KB (70,162 bytes)

's solution:

DATA: lv_file_size TYPE sapb-length.

lv_file_size = 0.

LOOP AT lt_file_data[] INTO ls_file_data.

  lv_file_size = lv_file_size + XSTRLEN( ls_file_data-line ) + 2.

ENDLOOP.

Result: lv_file_size = 70,794

's solution:

DESCRIBE TABLE lt_file_data[].

lv_file_size = sy-tfill * sy-tleng.

Result: lv_file_size = 70,656

's solution:

DESCRIBE TABLE lt_file_data[] LINES lv_file_tab_lines.

LOOP AT lt_file_data[] ASSIGNING <ls_file_data>.

  DESCRIBE FIELD <ls_file_data> LENGTH lv_file_line_length IN BYTE MODE.

  EXIT.

ENDLOOP.

lv_file_size = lv_file_tab_lines * lv_file_line_length.

Result: lv_file_size = 70,656

I must note that although each solution resulted in a different file size, eventually the image file was successfully archived and was viewable. I tried it several times and it seems that the archiving function does not really need the exact file size, so I really don't know how does the size affect its logic. When I gave it a significantly smaller value, like 700, the archived file was corrupt and unreadable. When I gave it a really large value, 700,000,000, the file was perfectly readable.

-------------------------------

My colleague have built this function module for the conversion process from XSTRING to TBL1024. Only now, while writing this post, I found the standard method CONVERT_XSTRING_TO_1022_RAW of class CL_RMPS_GENERAL_FUNCTIONS which provides the same functionality, but for TBL1022, which does not help me. So I copied its code and made the proper changes and got a good FM for my needs.


SAP's code, which I copied and changed to match TBL1024 (the original code is in the links above):


FUNCTION z_convert_xstring_to_tbl1024.

*"--------------------------------------------------------------------

*"*"Local Interface:

*"  IMPORTING

*"     REFERENCE(IM_XSTRING) TYPE  XSTRING

*"     REFERENCE(IM_LEN)     TYPE  I DEFAULT 0

*"  EXPORTING

*"     REFERENCE(RE_TAB_1022) TYPE  Z_TBL1024_TT

*"  EXCEPTIONS

*"      EMPTY_FILE

*"--------------------------------------------------------------------

* Task of this function: Conversion Xstring to 1024 Raw table

* Data declaration

  DATA:

        ls_xline_1024 TYPE TBL1024,

        l_xstring TYPE xstring,

        lv_abs_char TYPE i,

        lv_act_char TYPE i.

  CONSTANTS: lc_separator TYPE i VALUE '1024'.

* Processing block necessary ?

  IF im_xstring IS INITIAL.

    RETURN.

  ENDIF.

* Preparations

  MOVE im_xstring TO l_xstring.         "otherwise shift not possible

  IF im_len IS INITIAL.

    lv_abs_char  = xstrlen( l_xstring ). "number of separations

  ELSE.

    lv_abs_char = im_len.

  ENDIF.

* Conversion

  DO.

    MOVE l_xstring TO ls_xline_1024-line.

    APPEND ls_xline_1024 TO re_tab_1024.

    SHIFT l_xstring LEFT BY lc_separator PLACES IN BYTE MODE .

    CLEAR ls_xline_1024.

    lv_act_char = lv_act_char + lc_separator.

    IF lv_act_char > lv_abs_char.

      EXIT.

    ENDIF.

  ENDDO.

ENDFUNCTION.



My colleague's code:


FUNCTION z_convert_xstring_to_tbl1024.

*"--------------------------------------------------------------------

*"*"Local Interface:

*"  IMPORTING

*"     REFERENCE(IV_FILE_DATA) TYPE  XSTRING

*"  EXPORTING

*"     REFERENCE(ET_FILE_DATA) TYPE  Z_TBL1024_TT

*"  EXCEPTIONS

*"      EMPTY_FILE

*"--------------------------------------------------------------------

  DATA: ls_file_data        LIKE LINE OF et_file_data[],

        lv_file_length      TYPE i,

        lv_1024_chunks_amnt TYPE i,

        lv_remainder_chunk  TYPE i,

        lv_multiplier       TYPE i,

        lv_line_counter     TYPE i VALUE 0.

  lv_file_length = XSTRLEN( iv_file_data ).

  IF lv_file_length IS INITIAL.

    RAISE empty_file.

    EXIT.

  ENDIF.

  lv_1024_chunks_amnt = lv_file_length DIV 1024.

  IF lv_file_length > 0.

    lv_remainder_chunk = lv_file_length - ( lv_1024_chunks_amnt * 1024 ).

    " Splitting the big XSTRING text into 1024-characters chunks, that

    " will be saved as lines in the RAW 1024 table:

    DO lv_1024_chunks_amnt TIMES.

      CLEAR ls_file_data.

      lv_multiplier = lv_line_counter * 1024.

      ls_file_data-line = iv_file_data+lv_multiplier(1024).

      APPEND ls_file_data TO et_file_data[].

      lv_line_counter = lv_line_counter + 1.

    ENDDO.

    " Adding the remainder chunk of string:

    CLEAR ls_file_data.

    lv_multiplier = lv_line_counter * 1024.

    ls_file_data-line = iv_file_data+lv_multiplier(lv_remainder_chunk).

    APPEND ls_file_data TO et_file_data[].

  ELSE.

    ls_file_data-line = iv_file_data.

    APPEND ls_file_data TO et_file_data[].

  ENDIF.

ENDFUNCTION.


Sandra_Rossi
Active Contributor
0 Kudos

You should better write a blog post. Of course, file size is important, some formats or applications can deal with extra bytes, but of course you must always use the real file length. To convert an XSTRING to an internal table of type X, it's useless writing your own function module, use the generic function module SCMS_XSTRING_TO_BINARY.

former_member241258
Active Participant
0 Kudos

can u follow bellow steps u can it

step1 : find out Number of records in table.

step2: find basic size of table through debug (see in image)

in above image showing lt_date value like

LT_DATA  Standard table [209 * 1(2048)]

take value of 2048.

step3 : size = 2048 * no of records in table.

this is correct way to find size of table