1

I tried following the example to open existing Presentation given here in the documentation for testing. It works fine when the supplied presentation file is from local disk.

So now I want to open a Presentation file which is fetched from url. But createPresentation throws error said "An internal error has occured".

This is what I've tried

// function to open template
function openTemplate(assetID) {
    let downloadUrl = null;

    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: `${baseapiurl}api/v1/asset/download`,
        data: {
            'asset_id': assetID
        },
        headers: {
            'Authorization': `Bearer ${usertoken}`
        },
        async: false,
    })
        .done(function (response) {
            if (response.status == 'failed') {
                Office.context.ui.displayDialogAsync('Asset not found.');
                return;
            }

            downloadUrl = response.data.url;
        })
        .fail(function (response) {
            console.log('download request failed');
            console.log('Status: ' + response.status);
            console.log('Message: ' + response.statusText);
        });

    if (!downloadUrl) {
        return;
    }

    createPresentationFromBlob(downloadUrl, function (base64Pres) {
        console.log('enter callback');
        PowerPoint.createPresentation(base64Pres);
    });
}

function createPresentationFromBlob(url, function_callback) {
    fetch(url)
        .then(res => res.blob())
        .then(blob => {
            // console.log(blob instanceof Blob)
            const reader = new FileReader();
            reader.onloadend = (e) => {
                console.log(reader.result);
                // Remove the metadata before the base64-encoded string.
                const startIndex = reader.result.toString().indexOf("base64,");
                const copyBase64 = reader.result.toString().substring(startIndex + 7);

                console.log('calling callback');
                function_callback(copyBase64);
            };

            // Read in the file as a data URL so we can parse the base64-encoded string.
            reader.readAsDataURL(blob);
        })
        .catch(function (error) {
            console.log('createPresentationFromBlob error.');
            console.log(error.toString());
        });
}

Here PowerPoint.createPresentation(base64pres) throws error. How do I open a presentation file downloaded from remote source?

Alven RS
  • 11
  • 3

1 Answers1

0

Move the Powerpoint Creation procedure inside .done so that you can get downloadUrl.

function openTemplate(assetID) {
    let downloadUrl = null;

    $.ajax({
            type: 'POST',
            dataType: 'json',
            url: `${baseapiurl}api/v1/asset/download`,
            data: {
                'asset_id': assetID
            },
            headers: {
                'Authorization': `Bearer ${usertoken}`
            },
            async: false,
        })
        .done(function(response) {
            if (response.status == 'failed') {
                Office.context.ui.displayDialogAsync('Asset not found.');
                return;
            }

            downloadUrl = response.data.url;
            if (!downloadUrl) {
                return;
            }

            createPresentationFromBlob(downloadUrl, function(base64Pres) {
                console.log('enter callback');
                PowerPoint.createPresentation(base64Pres);
            });
        })
        .fail(function(response) {
            console.log('download request failed');
            console.log('Status: ' + response.status);
            console.log('Message: ' + response.statusText);
        });


}
Shri Hari L
  • 4,551
  • 2
  • 6
  • 18