cancel
Showing results for 
Search instead for 
Did you mean: 

MDK SAP Asset Manager - Convert attachment to Base64

former_member601123
Participant

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.

Accepted Solutions (0)

Answers (1)

Answers (1)

kevin_xu4
Active Participant
0 Kudos

Hi Sergio,

We found that you have created a ticket 481260/2021 to SAP about this issue and our engineer has answered.

So can you help to close this question?

Thanks for your cooperation,

Best Regards,

Kevin