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

Validate Input file Uploaded from the Presentation Server - Text Or Binary

Muthu_raja
Active Participant
0 Likes
1,581

Once the File has been uploaded from the presentation server, I tried to analyze the data whether it is in Binary format or not. It was an basic checks on the input data like below.

     if ( lv_data co '0123456789abcdefABCDEF' ).
        " Binary format
     else
        " Text format
     endif.

But Is there any other standard solution or utitlity class will be there to check the binary data ??

Thanks in Advance

1 ACCEPTED SOLUTION
Read only

Former Member
1,342

Firstly, your check for binary format is incorrect. If that passes you will have a text file that contains the characters 0-9 and a-f,A-F with no linefeeds or carriage returns.

A binary file is a file that contains bytes with an ascii value between 0-255. A text file would contain values from Ascii 32 (Space) to Ascii 122 (z) plus the possibility of Linefeeds (10), Carriage returns (13) and tabs(09). You can use class cl_abap_char_utilities to get these ascii values.

So I would check for a text file and assume everything else is a binary file. I would also use a regex, rather than the SAP string searches.

Rich

5 REPLIES 5
Read only

Former Member
1,343

Firstly, your check for binary format is incorrect. If that passes you will have a text file that contains the characters 0-9 and a-f,A-F with no linefeeds or carriage returns.

A binary file is a file that contains bytes with an ascii value between 0-255. A text file would contain values from Ascii 32 (Space) to Ascii 122 (z) plus the possibility of Linefeeds (10), Carriage returns (13) and tabs(09). You can use class cl_abap_char_utilities to get these ascii values.

So I would check for a text file and assume everything else is a binary file. I would also use a regex, rather than the SAP string searches.

Rich

Read only

1,342

muthuraja.muthaiah I agree with Richard: "your check for binary format is incorrect".

I'd give a different definition though, but I can only define what is a "text file" (a "binary file" is "what is not a text file", although we could also have "mixed files", which happens very frequently).

A text file contains only characters encoded in one given character set (A.K.A. code page) and is like a book humans can read and find a meaning.

Note that even a text or binary file is not sufficient to identify the nature of a file, which may be very different: a text file might contain plain text, CSV format, XML, and so on, and a binary file might be a PDF, XLSX, and so on.

99.9% of time, the program knows that the input file it has to process should be text or binary without even reading it first, and the program should parse and validate the file assuming a given format, and should trigger an error if it fails.

So, the big question is, why do you need to choose between "text" and "binary" ?

PS: the only frequent detection which might resemble your question is to recognize the code page of a text file uploaded by a user, because it may differ a lot although the decoded characters might be the same (for the word "café", English and French code pages would encode "é" differently)

Read only

1,342

Hi Sandra,

"Note that even a text or binary file is not sufficient to identify the nature of a file, which may be very different: a text file might contain plain text, CSV format, XML, and so on,"

True, but regardless of the format of the information inside the file, it is still a text file. I would classify a 'mixed' file as binary.....

Rich

Read only

0 Likes
1,342

Thanks richard.harper and sandra.rossi for your answers.

@ sandra.rossi I wanted to send mail with an attachments, I was using cl_bcs wrapper class and there will be two different parameters for add_attachment method ( text & binary ). So there might be a chance any type of files will be uploaded by User. I wanted to analyze the input file uploaded and call method parameters accordingly. This brought me to ask this question.

Thanks Again.

Read only

1,342

muthuraja.muthaiah If you pass the uploaded file to CL_BCS, even if you don't know its format, just pass it unchanged, i.e. in binary mode (the only possible issue would be that the recipient could not read correctly the attachment if it's a text file and the user has a different character set, but anyway that's a well know issue that is not even handled by our favorite email softwares) :

doc->ADD_ATTACHMENT(

I_ATTACHMENT_TYPE = 'EXT'

I_ATTACHMENT_SIZE = file_size_in_bytes

I_ATT_CONTENT_HEX = file_contents

I_ATTACHMENT_HEADER = value #( ( line = '&SO_FILENAME=' && file_name ) ) ).