Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM forum
Suggested answer

Generate file on button click

Posted on by 175

Hello,

I have a button on record, on click of that button it should fetch some data and download it in excel for the user.

I have a wcf service which is called on that button for fetching the data.

I need ideas on how to generate and download the file for end user.

Any help ?

Thanks.

Categories:
  • Suggested answer
    gdas Profile Picture
    gdas 50,083 on at
    RE: Generate file on button click

    Hi,

    Return your response text  or file content in Base64 format in your WCF service like below in response object .

       Response.FileContent = Convert.ToBase64String(_fileContentText);
    Response.FileName ="Test.Xls";


    In the client side here is the sample code to consume function and download the file -

      var objRequest = {
                            Parameter1 : hdrType,
    Parameter2 : FileType. };


    jQuery.support.cors = !0, $.ajax({
        url: WCFServiceURL,
        data: JSON.stringify(objRequest),
        type: "POST",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        cache: !1,
        async: true,
        success: function (ResponseObj) {
            if (ResponseObj != null && ResponseObj != undefined) {
                var fileCont = ResponseObj.FileContent;
                var fileName = ResponseObj.FileContent;       
                var samplebyte = base64ToArrayBuffer(fileCont);
                saveByteArray([samplebyte], fileName);
            }
        },
        error: function (error) {
            alert(error);
        }
    });
    
    
    function base64ToArrayBuffer(base64) {
        var binaryString = window.atob(base64);
        var binaryLen = binaryString.length;
        var bytes = new Uint8Array(binaryLen);
        for (var i = 0; i < binaryLen; i++) {
            var ascii = binaryString.charCodeAt(i);
            bytes[i] = ascii;
        }
        return bytes;
    }
    
    var saveByteArray = (function () {
        var a = document.createElement("a");
        document.body.appendChild(a);
        a.style = "display: none";
        return function (data, name) {
            var blob = new Blob(data, { type: "octet/stream" }),
                url = window.URL.createObjectURL(blob);
            window.navigator.msSaveOrOpenBlob(blob, name);
        };
    }());


    Hope this helps.

  • Tomas Prokop Profile Picture
    Tomas Prokop 590 on at
    RE: Generate file on button click

    What version of Dynamics 365 are you running? it is possible to manage JS dependencies in 9.0.
    chrome_5F00_2018_2D00_05_2D00_27_5F00_17_2D00_19_2D00_25.png

    What type of Web Resource is your script? JScript or HTML Webpage with button?

  • LBA Profile Picture
    LBA 175 on at
    RE: Generate file on button click

    Hi Tomas,

    I have added FileSaver.js on the form and added these two lines. I am getting error saying 'saveAs' is undefined. It seems it unable to reference the file. Am I missing anything ?

  • Suggested answer
    Tomas Prokop Profile Picture
    Tomas Prokop 590 on at
    RE: Generate file on button click

    Look at FileSaver.js github.com/.../FileSaver.js

    var blob = new Blob([byte array fom web service], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
    
    FileSaver.saveAs(blob, "hello.xlsx");


  • LBA Profile Picture
    LBA 175 on at
    RE: Generate file on button click

    Hi,

    I have created excel in a memorysteam in wcf and returned it. I am now looking for a way to make it downloadable from javascript.

  • gdas Profile Picture
    gdas 50,083 on at
    RE: Generate file on button click

    Hi ,

    Hope you exposed wcf service as rest enabled and then using javascript make XmlHttpRequest   receive the result ,once you get the  result just loop through each record and add the data.in the workbook column.

  • LBA Profile Picture
    LBA 175 on at
    RE: Generate file on button click

    I am calling wcf service from javascript but I am not sure what should be returned from the service whether returning stream is a good idea or json and then after creating file from it.

  • Suggested answer
    gdas Profile Picture
    gdas 50,083 on at
  • Suggested answer
    Tomas Prokop Profile Picture
    Tomas Prokop 590 on at
    RE: Generate file on button click

    Hi,

    you have two options:

    1) Do it all client side and use something like this: github.com/.../excel-builder.js

    2) Go server side. It is a way easier to consume WCF from .NET.

    • Create an Action and call your custom .NET plugin which will generate the output file and return it as Base64.
    • Call the action from the client side using process.js.
    • Use a mechanism like FileSaver.js to support larger files github.com/.../FileSaver.js

    It can be quick and easy for a skilled Dynamics dev but I understand you can have a hard time plugging this together if you don't work with it on a daily basis.

    If you were lost I could help you with the implementation.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Anton Venter – Community Spotlight

Kudos to our October Community Star of the month!

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,558 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,647 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans