Announcements
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.
*This post is locked for comments
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.
What version of Dynamics 365 are you running? it is possible to manage JS dependencies in 9.0.
What type of Web Resource is your script? JScript or HTML Webpage with button?
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 ?
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");
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.
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.
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.
Hi ,
This is similar I have used in my projects , try with this -
https://www.igniteui.com/help/javascript-excel-library-create-a-workbook
www.igniteui.com/.../javascript-excel-library-save-and-load-files-in-excel-template-format
help.infragistics.com/.../Using_the_Client_Side_Excel_Library.html
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.
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.
André Arnaud de Cal... 291,359 Super User 2024 Season 2
Martin Dráb 230,370 Most Valuable Professional
nmaenpaa 101,156