Skip to Content
0
Jul 07 at 10:14 PM

MDK SAP Asset Manager - Convert attachment to Base64

326 Views

Hello,

I'm trying to read the attachments of a work order from a custom page. I have found a way to get the attachments list

 {
            "Header": {
                "UseTopPadding": true
            },
            "EmptySection": {
                "Caption": "$(L,no_documents_available)"
            },
            "ObjectCell":
            {
                "PreserveIconStackSpacing": true,
                "Icons": "/SAPAssetManager/Rules/Documents/DocumentListViewIconImages.js",
                "ProgressIndicator": "/SAPAssetManager/Rules/Documents/SetIndicatorStateWrapper.js",
                "DetailImage": "/SAPAssetManager/Rules/Documents/DocumentTypeIcon.js",
                "DetailImageIsCircular": false,
                "Title": "/SAPAssetManager/Rules/Documents/DocumentFileName.js",
                "Subhead": "/SAPAssetManager/Rules/Documents/DocumentDescription.js",
                "Footnote": "/SAPAssetManager/Rules/Documents/DocumentFileSizeWrapper.js",
                "OnAccessoryButtonPress": "/ZSAMComponent/Rules/PDF/Prueba.js"
            },
            "Target":
            {
                "EntitySet" : "/SAPAssetManager/Rules/Documents/DocumentsBDSListEntitySet.js",
                "Service" : "/SAPAssetManager/Services/AssetManager.service",
                "QueryOptions": "/SAPAssetManager/Rules/Documents/DocumentsBDSQueryOptions.js"
            },
            "_Type" : "Section.Type.ObjectTable",
            "_Name": "DocumentSection"
        }

Now using a the script below I was able to open the selected attachment.

export default function Prueba(context) {

    let pageProxy = context.getPageProxy();

    let documentObject = pageProxy.getActionBinding()["Document"];

    let value = pageProxy.binding['@odata.readLink'] + '/WODocuments';

    let readLink = documentObject['@odata.readLink'];
    let serviceName = '/SAPAssetManager/Services/AssetManager.service';
    let entitySet = readLink.split('(')[0];
    const docDownloadID = 'DocDownload.' + documentObject.DocumentID;

     return pageProxy.isMediaLocal(serviceName, entitySet, readLink).then((isMediaLocal) => {
        if (isMediaLocal) {
            let documentPath = DocumentPath(pageProxy, documentObject);
            let writeError = undefined;
            let promise = Promise.resolve(documentPath);

            if (!File.exists(documentPath)) {
                promise = pageProxy.executeAction('/SAPAssetManager/Actions/Documents/DownloadMedia.action').then(() => {
                    // the media has been downloaded, we can open it -> the path needs to be provided in the action definition
                    // or it should come from binding
                    let documentFileObject = File.fromPath(documentPath);
                    let content = pageProxy.getClientData()[documentObject['@odata.readLink']];

                    const binarySource = documentFileObject.readSync(content,(err) => {
                        console.log(err);
                    });

                    

                    if (libVal.evalIsEmpty(documentPath) || typeof documentObject === 'undefined') {
                        return pageProxy.executeAction('/SAPAssetManager/Actions/Documents/DownloadDocumentStreamsFailure.action');
                    }

                    documentFileObject.writeSync(content, err => {
                        writeError = err;
                    });


                    let binary = documentFileObject.value.readSync(err => { console.log("Error:" + err); });
                    console.log("Binario: "+this.binary);

                    libCommon.clearFromClientData(pageProxy, docDownloadID, undefined, true);
                    return documentPath;
                    
                });
            }
            return promise.then(docPath => {
                if (writeError) {
                    actionPath = '/SAPAssetManager/Actions/Documents/DownloadDocumentStreamsFailure.action';
                }
                pageProxy.setActionBinding({
                    'FileName': docPath,
                });
                let actionPath = '/SAPAssetManager/Actions/Documents/DocumentOpen.action';
                return pageProxy.executeAction(actionPath);
            });
        }
    });
}

I'm trying to use this code snippet to read the attachment content in order to use it for the base64 conversion.

let binary = documentFileObject.value.readSync(err => { console.log("Error:" + err); });  console.log("Binary: "+this.binary);

However, it seems to be undefined.

What I need to do is:

1. To read one single attachment (.jpg or .png )with certain characteristics via scripting instead of using the Target path on the page and avoid the usage of the OnAccessoryButtonPress

2. To convert the attachment (.jpg,.png) to Base64. Is there an MDK function to do it?

I appreciate your suggestions.