How to download a file using a button on a ribbon on a Model Driven App with C# Plugins?

34 Views Asked by At

I managed to integrate code that allows me to download a file from a Dataverse table within a plugin and I can perform automation tasks on this document

private static byte[] DownloadFile(IOrganizationService orgService, string entityName, Guid recordGuid, string fileAttributeName)
{
    var initializeFileBlocksDownloadRequest = new InitializeFileBlocksDownloadRequest
    {
        Target = new EntityReference(entityName, recordGuid), FileAttributeName = fileAttributeName
    };

    var initializeFileBlocksDownloadResponse = (InitializeFileBlocksDownloadResponse)orgService.Execute(initializeFileBlocksDownloadRequest);

    DownloadBlockRequest downloadBlockRequest = new DownloadBlockRequest
    {
        FileContinuationToken = initializeFileBlocksDownloadResponse.FileContinuationToken
    };

    var downloadBlockResponse = (DownloadBlockResponse)orgService.Execute(downloadBlockRequest);
            
    return downloadBlockResponse.Data;
}

But after I've completed my automation task I wish to download this file to the local download directory of the person who triggered the plugin, from what I know you can use File.WriteAllBytes(<File_Name>, downloadBlockResponse.Data); but its not working, is there any workaround or direct methods of downloading a file using a plugin?

1

There are 1 best solutions below

1
Henk van Boeijen On

You can't. Plugin code is running on the server side. The server has no access to a user's local storage. If it would have access, this would be a major security issue.

However, the InitializeFileBlocksDownloadRequest and DownloadBlockRequest can actually be invoked from JavaScript. Write a script that uses these requests to get the data and that can be invoked form a ribbon button.