SAP Cloud Application Programming Model (CAP): simple file upload service using node.js
Description of this post
Why a file upload service? Do we really need this? Yes and no.
You can use the media handling functionalities on an entity’s attribute… (CDS definition -> @Core.MediaType annotation) this can be done very quickly but you’ll get basically an upload service that will record the file content in your database…
I want to process the content of the file, use it for different entities’ feeding, correct errors, format some fields… this seems far more simple using a dedicated service build on my own via a node.js entry point.
Client side UI5 page elements
Xml view: to upload a file, the “FileUploader” component is used, with this configuration:
Points of attention (that will be used on the server side later):
- “Name” attribute => “UploadedFile”
- uploadUrl => “/UploadFile”
- httpRequestMethod => “POST”
Controllers: the most important handlers:
Server side layer
First let’s create the definition of the service… Since CAP is based on the “express” web framework, we need to add our own version of the “server.js” file in our application directory (“/srv” directory)…
I’ll use two libraries/additional packages to deal with the file upload and the file parsing (the file will be a simple CSV file):
=> In the “server.js” file:
- const fileUpload = require(‘express-fileupload’)
- const Papa = require(‘papaparse’)
Full content of the “server.js” file:
Point of interest:
Var sKeysOfEntity = Object.keys(BankOperationsBankIng.elements);
=> Extract attributes of the CDS entity to allow a mapping with the CSV file fields… (via “transformHeader” inline function)
Thanks for reading,
My question is a little bit out of this scenario, is that possible to upload the csv file directly on CDS layer?
For ex: customer can use CAP model as inbound API process, to import their csv to my HANA table.
Appreciated if you can share some thoughts
When the application is deployed to SAP BTP, when you press the upload button after selecting a CSV file, it gives 403 Forbidden error although the file gets uploaded successfully in the local environment. How did you fix this issue?