Let's follow up this discussion at the Neptune Community

(this forum will be kept in read-only mode)

Downloading a file(image/pdf) to phone from GOS attachment in SAP

Hi

I have a requirement that I need to download an attachment created from SAP GOS Service(I think the data would be stored in BIN format) to my phone. The attachment could be big and might lead to crash of the app.


Any idea how can this be achieved. Is there any cordova plug-in for this?


Regards,

Rahul.


Hi Rahul,


Are the attachments stored in the content server?


Easy solution:

There is a function module ARCHIVOBJECT_GET_URI to obtain the URL to the content server attachments.

You can then use a sap.m.Link and bind the link to the src property of the link object, and the download will start immediately. No coding required.


If the easy solution is not appropiate, we must send the binary data from the Application Class and then open it as an attachment on the client side.


The format desired on devices is xstring (binary data) + it must be base64 encoded. Use SCMS_BASE64_ENCODE_STR to format XSTRING to Base64.


Example on the Application Class to send back binary data for a PDF:




1 person likes this

Thank you Julian,

 

This helps us on the way, but it still does not work in the app.

It just quitely fails.

 

We see that a element is created in the DOM with reference to a blob

<a href="blob:file%3A///cf2e336c-8c10-4e54-9e99-26f7d5a0115f" download="1" style="display: none;"></a>

 

We suspect that Cordova is blocking the file download.

We’ve tried setting the following in the config.xml before uploading to phonegap build, but it has no effect.

<access origin="blob:*"/> or

<access origin="blob:*" launch-external="yes" />

 

For Cordova 4.0 someone has had success with

<allow-navigation href="*" />

<allow-navigation href="blob:*" />

http://stackoverflow.com/questions/31941634/epub-js-phonegap-not-working-because-of-blob-uri

 

Unfortunately, we’re on cordova 3.6.5 and the plugin https://github.com/apache/cordova-plugin-whitelist is only for 4.0 and above.

 

Have you any experience file download in an app ?


Hi Rahul


Is it possible to upgrade to latest cli-5.2.0 phonegap ?


I have attached a code we use to download PDF file. We then use the fileopener plugin to open the file from local storage. Maybe this will help you:


 

// ---------------------------------------------------
// Open mobile native PDF Reader
// ---------------------------------------------------
sap.n.PDF.NativeOpen = function() {

    // Close Dialog
    var dia = AppCache.View[AppCache.CurrentApp].getParent();

    setTimeout(function() {
        dia.close();
    }, 10);


    // Set Directory
    switch (sap.ui.Device.os.name) {

        case 'Android':
            sap.n.PDF.NativeDir = cordova.file.externalCacheDirectory;
            break;

        case 'iOS':
            sap.n.PDF.NativeDir = cordova.file.tempDirectory;
            break;

        case 'winphone':
            sap.n.PDF.NativeDir = cordova.file.externalCacheDirectory;
            break;

        default:
            break;

    }

    // Create Array
    sap.n.PDF.DataArray = sap.n.PDF.MakeBinary(sap.n.PDF.Data);

    // Create and Display File
    window.resolveLocalFileSystemURL(sap.n.PDF.NativeDir, function(dir) {
        dir.getFile("pdfViewer.pdf", {
            create: true
        }, function(file) {
            sap.n.PDF.NativeWriteLog(file);
        });
    });
}

sap.n.PDF.NativeWriteLog = function(file) {

    file.createWriter(function(fileWriter) {

        fileWriter.onwriteend = function(e) {
            cordova.plugins.fileOpener2.open(
                sap.n.PDF.NativeDir + "pdfViewer.pdf",
                "application/pdf", {
                    error: function(e) {
                        console.log('Error open: ' + e.status + ' - Error message: ' + e.message);
                    }
                }
            );
        };

        fileWriter.onerror = function(e) {
            console.log('WRITE ERROR is');
            console.log(e);
        };

        var blob = new Blob([sap.n.PDF.DataArray], {
            type: 'text/pdf'
        });
        fileWriter.write(blob);

    }, sap.n.PDF.NativeWriteFail);
}

sap.n.PDF.NativeWriteFail = function(e) {
    console.log('Error write: ' + e.status + ' - Error message: ' + e.message);
}

 


Regards

Ole Andre 

Login or Signup to post a comment