File Uploads, Attachments & Media Handling in CAP

Modern applications often require handling files, attachments, and media content. SAP CAP provides built-in support for managing binary data through OData media entities, associations, and service handlers.

This guide explains how to upload files, manage attachments, and handle media efficiently in CAP applications.

Why File & Media Handling Matters

CAP applications often need to support:

  • Document uploads (PDF, Word, Excel)
  • Images and media attachments
  • Large binary files
  • User-generated content

Proper handling ensures:

  • Secure storage
  • Efficient retrieval
  • Integration with OData services
  • Scalable solutions for enterprise apps

OData Media Entities

CAP uses media entities to handle binary content. Media entities are linked to standard CDS entities but store content separately.

Example: Document Entity

entity Documents @(Capabilities.MediaType) {
  key ID : UUID;
  name : String;
  mimeType : String;
}

CAP automatically exposes:

  • $value endpoint for binary data
  • CRUD operations for metadata (name, mimeType)
  • Upload/download capabilities via OData

Uploading Files via CAP Service Handler

Example using Node.js CAP service:

const cds = require('@sap/cds');

module.exports = cds.service.impl(async function () {
  const { Documents } = this.entities;

  this.on('UPLOAD', async req => {
    const file = req.data.file; // binary data
    const metadata = {
      name: req.data.name,
      mimeType: req.data.mimeType
    };
    await INSERT.into(Documents).entries(metadata);
    // Store file in database or storage service
    return { status: 'success', name: metadata.name };
  });
});

Handling Attachments

CAP allows associating files with other entities via associations.

Example: Product with Attachments

entity Products {
  key ID : UUID;
  name : String;
  attachments : Composition of many ProductAttachments on attachments.product = $self;
}

entity ProductAttachments @(Capabilities.MediaType) {
  key ID : UUID;
  product : Association to Products;
  fileName : String;
  mimeType : String;
}
  • Parent entity manages lifecycle of attachments
  • Supports cascading deletes
  • Enables $expand queries for metadata

Storing Media Outside the Database

For large files, storing media in the database is not efficient. Use:

  • SAP BTP Object Store
  • External storage services (S3, Azure Blob)
  • CAP handlers to upload/download files via APIs

Example: Upload to Object Store

const objectStore = require('./objectStoreClient');

this.on('UPLOAD', async req => {
  const file = req.data.file;
  const key = await objectStore.upload(file, req.data.name);
  await INSERT.into(Documents).entries({ name: req.data.name, mimeType: req.data.mimeType, url: key });
});

Downloading Files

Download via OData $value endpoint:

GET /odata/v4/Documents(ID)/$value

Or via custom handler:

this.on('DOWNLOAD', async req => {
  const doc = await SELECT.one.from(Documents).where({ ID: req.data.ID });
  const file = await objectStore.download(doc.url);
  return file;
});

Security & Access Control

  • Use XSUAA and RBAC for file access
  • Restrict download/upload to authorized users
  • Validate MIME types and file size
  • Scan uploaded files for malware

Best Practices

  • Use media entities for small to medium files
  • Use external storage for large files (>5MB)
  • Separate metadata from binary content
  • Always validate and sanitize file uploads
  • Implement streaming for large file transfers
  • Secure APIs with scopes and roles

Real-World Example

  1. User uploads PDF invoice
  2. CAP service stores metadata in HANA Cloud
  3. Binary file stored in Object Store
  4. OData service exposes $value for download
  5. RBAC ensures only authorized users access the file

Conclusion

CAP provides robust options for file uploads, attachments, and media handling. Using media entities, compositions, and external storage, developers can build scalable, secure, and maintainable solutions for document and media management in enterprise applications.

you may be interested in this blog here:-

Don’t Fear the Update: Navigating the Challenges of how to implement sap note

Five Top Technology Investment Drivers for 2024

How many dollars worth of RSU does Salesforce typically offer an MTS (experienced hire) on joining?

Integration cloud system to HANA Cloud Platform using Cloud Connector

₹25,000.00

SAP SD S4 HANA

SAP SD (Sales and Distribution) is a module in the SAP ERP (Enterprise Resource Planning) system that handles all aspects of sales and distribution processes. S4 HANA is the latest version of SAP’s ERP suite, built on the SAP HANA in-memory database platform. It provides real-time data processing capabilities, improved…
₹25,000.00

SAP HR HCM

SAP Human Capital Management (SAP HCM)  is an important module in SAP. It is also known as SAP Human Resource Management System (SAP HRMS) or SAP Human Resource (HR). SAP HR software allows you to automate record-keeping processes. It is an ideal framework for the HR department to take advantage…
₹25,000.00

Salesforce Administrator Training

I am text block. Click edit button to change this text. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.
₹25,000.00

Salesforce Developer Training

Salesforce Developer Training Overview Salesforce Developer training advances your skills and knowledge in building custom applications on the Salesforce platform using the programming capabilities of Apex code and the Visualforce UI framework. It covers all the fundamentals of application development through real-time projects and utilizes cases to help you clear…
₹25,000.00

SAP EWM

SAP EWM stands for Extended Warehouse Management. It is a best-of-breed WMS Warehouse Management System product offered by SAP. It was first released in 2007 as a part of SAP SCM meaning Supply Chain Management suite, but in subsequent releases, it was offered as a stand-alone product. The latest version…
₹25,000.00

Oracle PL-SQL Training Program

Oracle PL-SQL is actually the number one database. The demand in market is growing equally with the value of the database. It has become necessary for the Oracle PL-SQL certification to get the right job. eLearning Solutions is one of the renowned institutes for Oracle PL-SQL in Pune. We believe…
₹25,000.00

Pega Training Courses in Pune- Get Certified Now

Course details for Pega Training in Pune Elearning solution is the best PEGA training institute in Pune. PEGA is one of the Business Process Management tool (BPM), its development is based on Java and OOP concepts. The PAGA technology is mainly used to improve business purposes and cost reduction. PEGA…
₹27,000.00

SAP PP (Production Planning) Training Institute

SAP PP Training Institute in Pune SAP PP training (Production Planning) is one of the largest functional modules in SAP. This module mainly deals with the production process like capacity planning, Master production scheduling, Material requirement planning shop floor, etc. The PP module of SAP takes care of the Master…
X
WhatsApp WhatsApp us
Call Now Button