“This is the 11th day of my participation in the Gwen Challenge in November. See details: The Last Gwen Challenge in 2021”

This is Jerry’s 71st article of 2021, and the 348th original article of Wang Zixi’s official account.

Jerry previously published an article about uploading local files to ABAP server by hand with pure JavaScript without using any framework. After that, many friends left comments and raised questions in the following two categories:

(1) The data sent by the client through the multipart/form-data format, ABAP side in addition to Jerry’s article to take a string parsing of the more cumbersome way to deal with, there are other methods?

(2) Can you upload binary files such as Excel to ABAP and parse them?

This article will answer these two questions.

Use JavaScript to send PDF and Excel files to the ABAP server in multipart/form-data format

For detailed instructions on the multipart/form-data format, refer to the Mozilla development community and W3 Org documentation:

  • Developer.mozilla.org/en-US/docs/…

  • www.w3.org/html/wg/spe…

I modified the previous example by using two input tags of type file in the Form to upload a PDF file and an Excel file:

The local PDF file used for testing: pdf.pdf, 30129 bytes in size.

As follows:

XLSX = test.xlsx = test.xlsx

Click on the hyperlink of the HTML page to upload a file. In Chrome Developer Tools, you can see the load of HTTP POST requests, including the binary streams contained in the PDF and Excel input controls:

Click view Source to view the multipart/form-data data details:

You can still see the file name and content-Type of the uploaded PDF and Excel file in Chrome Developer Tools. Unlike the previous example of uploading a text file, you cannot see the binary contents of the two files — the binary contents can be observed in the ABAP server-side debugger.

As an example of the above uploaded PDF file, the form-data data received on the ABAP server is shown in the following figure. The green highlighted area is the file name pdf. PDF and the file type application/ PDF that can be seen in Chrome developer tools above, and the binary content of the PDF file starting with %PDF-1.4#.

To be precise, PDF is a hybrid of text and binary streaming. When you open pdF.pdf with a text editor, you can see that the header of the file contains text characters describing the file metadata, such as the creation and modification time of the file, the name of the tool that created the file, etc., and the second half is the binary stream.

There are already a number of open source tools such as JavaScript libraries that can be used to generate and parse PDF files.

  • Several ways to generate PDF files using ABAP and JavaScript code
  • Use JavaScript to save the current page as A PDF, support to save pictures and text
  • How to convert PDF files to MarkDown format

For the file names of PDF files uploaded to the ABAP server, we parse them the same way we did in the previous article, extracting them from the string in the red rectangle below.

CL_HTTP_REQUEST provides a method to extract the binary PDF data highlighted in green. Key codes are shown in the figure below:

When the ABAP server receives multipart/form-data data from the client, the num_multiparts method of CL_HTTP_REQUEST can be used to obtain the number of parts, and then the get_multipart method can be used. Passing in an index for each part gives you an instance reference that represents that part.

Calling the reference’s get_content_type and get_data methods resolves the type of uploaded file (such as application/ PDF for PDF format) and binary content.

At this point, the three parameters of calling SAP CRM attachment creation API: file name, file type and file binary content are all ready. Calling API can create the uploaded PDF and Excel data as the attachment of SAP CRM sales order.

The created PDF and Excel attachments are displayed in the SAP CRM system as follows:

Open these two attachments and make sure they are exactly the same as the local file:

How to use ABAP to parse an uploaded Excel file

This topic, in fact, Jerry’s 2019 article has systematically introduced several methods of using ABAP to operate Excel.

We observed in the ABAP debugger that the Excel file with the local extension XLSX, when uploaded to the ABAP server, has the content-Type of: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

What is openxmlformats? The following are specific examples to illustrate.

Taking my local Excel file as an example, change the extension from XLSX to zip and unzip:

It turns out that the XLSX file is actually a compressed package, which when unzipped generates a folder containing several subfolders and files as shown below.

The Excel file above has a sheet called Sheet1, A1 with the value ABAP and B1 with the value Java. This information is stored in the sheet1.xml subfile of the worksheets folder:

The highlighted XML C node in the figure above represents the Cell. R =”A1″ and r=”B1″ represent the Row IDS of these two cells. The child node V of C contains the specific value of the Cell.

Sheet1.xml doesn’t include ABAP and Java string literals directly, but just indexes, 0 and 1. For those of you who have done Java development, you can compare this design to Java’s string constant pool.

In the unzipped folder there is another file, sharedstrings. XML, which, as the name implies, maintains all the strings present in Excel Worksheets for sharing between sheets. Each separate sheet XML file maintains only an index of the strings used to reduce the size of the Excel file.

Therefore, you can parse Excel files using any high-level programming language, as long as you are familiar with the purpose of each file generated after test.xlsx was renamed to test.zip and unzipped, the protocol specification for Open XML Formats.

You can find the purpose of each file defined by the Open XML Formats protocol in WikiPedia:

En.wikipedia.org/wiki/Office…

SAP CRM provides a utility class that parses Excel file content based on Open XML Formats: CL_xlsX_Document.

Simply pass in the binary content of an Excel file, and the utility class returns a reference to an Excel file. Based on the various GET methods of that reference, you can access the content of the various parts of an Excel file defined by the Open XML Formats protocol.

The core logic is shown in the figure below. The code is self-describing and will not be described here.

Of course, the open source project ABap2xLSx is another option:

Github.com/sapmentors/…

Jerry will cover the technical details of the SAP Fiori application uploading attachments through the SAP Gateway in the future.

The complete source code for the front and back ends covered in this article can be downloaded at this link.

Thanks for reading.

Jerry’s ABAP feature

  • Jerry’s ABAP, Java and JavaScript stew

  • What should ABAP developers learn in the future

  • Jerry’s 2017 May Day Holiday: ABAP implementations of eight classical sorting algorithms

  • Jerry’s collection of original ABAP technical articles

  • Three hundred lines of ABAP code implement a simplest blockchain prototype

  • Call functions in ABAP on-premise system using Java+SAP Cloud Platform +SAP Cloud Connector

  • Consume ABAP on-premise OData services in the CloudFoundry environment of SAP cloud platform

  • ABAP vs Java, breaststroke vs freestyle

  • Talk about C and ABAP

  • Use ABAP Channel to develop some small tools to improve daily work efficiency

  • All the boring things I’ve done with ABAP

  • Don’t like SAP GUI? Try using Eclipse for ABAP development

  • Write and activate ABAP Code using Visual Studio Code

  • Has your ABAP program been opened to Buddha? Let’s try Jerry’s trick

  • Write the first ABAP program in SAP cloud platform ABAP programming environment

  • ABAP programming specification issued by SAP

  • Do you know the hidden features of ABAP Code Inspector?

  • Are you using ABAP for secondary development of SAP products? Learn about this new concept of secondary development

  • The parasitic programming language inside ABAP Netweaver

  • Start with a blog post on the SAP community to talk about the sentiment behind SAP product naming

  • Cloud ABAP Restful service development

  • How to expose CDS View as OData service in SAP cloud platform ABAP programming environment

  • Use abapGit to transfer code between the ABAP ON-premises system and the SAP cloud platform ABAP environment

  • 30 minutes To develop a Fiori application with Restful ABAP Programming model to support add, delete, change, and query

  • Jerry walks you through the implementation of Action and Validation, part 2 of the Restful ABAP Programming Model series

  • Jerry takes you through Restful ABAP Programming Models series 3: Cloud ABAP Application debugging

  • How to consume third-party services in ABAP programming environment on SAP cloud platform

  • It’s time for ABAP developers to go to the cloud – now you can use a free trial version of the ABAP environment for SAP cloud Platform

  • The origin and application scenario of ABAP programming environment on SAP cloud platform

  • Trident application in SAP cloud platform

  • How to develop and deploy a Fiori application that supports add, delete, modify and query based on Restful ABAP Programming model

  • SAP 2019 TechEd Key Note: How do SAP practitioners do secondary development in the cloud era?

  • What ABAP keywords and syntax won’t work in the ABAP cloud environment?

  • The ABAP development environment finally supports automatic formatting of ABAP variable names with camel name

  • Complete a real work task using ABAP 740’s new REDUCE keyword

  • A shiver of ABAP code

  • Yesterday’s Halloween ABAP monster code mystery, revealed the answer

  • This paper introduces an efficient copy method of inner table in ABAP kernel state

  • A practical example of developing OData using the SAP Cloud Application Programming model

  • When ABAP met Prometheus

  • Draw scalable vector diagrams using ABAP

  • ABAP development environment syntax highlighting things

  • Seven weapons of SAP error message debugging: Make all error messages locable

  • Several methods of using ABAP to operate Excel

  • SAP GUI in the favorites transaction code management tool

  • SAP GUI and Windows registry

  • Can you do bad things with Debug permission? Be careful. Your every move is on the system

  • ABAP CCDEF, CCIMP, CCMAC, CCAU, CMXXX

  • Three ways to implement ABAP conditional breakpoints

  • Use SAT trace to monitor the performance and call stack of SAP applications opened from the browser

  • Advice from a 13-year ABAP veteran: Knowing the basics will never hurt ABAP development

  • SAP ABAP Netweaver containerization, impossible task?

  • SAP Product enhancement Technology Review

  • SAP API development methods

  • Brief introduction to Java and SAP ABAP static proxy and dynamic proxy, and ABAP tangential programming attempt

  • SAP ABAP application server HTTP response Status Code

  • Is there a Java List collection utility class in SAP ABAP? CL_OBJECT_COLLECTION take a look

  • ABAP interview question series: Write an ABAP procedure that creates a Deadlock

  • This section describes how to log in to the SAP ABAP Netweaver server

  • SAP ABAP keyword syntax diagram and ABAP Code Composer automatic generation tool

  • Alternative use of SAP ABAP SM50 – Detection of database table reads by ABAP worker processes

  • SAP ABAP character variable and string variable character number of a knowledge point, and a blood case

  • SAP ABAP IS BOUND, IS NOT INITIAL and IS ASSIGNED

  • Weakreferences and Soft References in SAP, ABAP and Java

  • Introduction to SAP AMDP – ABAP hosted HANA database process

  • Tag your ABAP object

  • On this day in history: A billion dollar bug for null references in programming languages

  • A summary of ABAP Development Tool code templates and some other practical tips

  • SAP ABAP Development Tool 10 Tips for improving Development efficiency

  • How to consume SOAP-based Web Services in ABAP programming environment of SAP BTP platform

  • Is ABAP really going out of style? Talk about ABAP’s past, present and future

  • An example of ABAP continuous integration based on abapGit and abaplint

  • Hand-written pure JavaScript implementations upload local files to ABAP servers without using any framework

For more of Jerry’s original articles, see “Wang Zixi “: