1
0
-1

I have created a dynamic service which creates spreadsheet dynamically and then I am attaching it to the form submission. That attachment could be downloaded immediately on the same form using download button (Calls a dynamic service which returns a attachment key for downloading). Is this the correct way or any better alternative for implementing the same? 

    CommentAdd your comment...

    1 answer

    1.  
      1
      0
      -1

      I'm not sure if I understand your scenario 100%... however, I have done something similar.  I had a DDS which returned a CSV which I wanted the user to be able to download from the form.  I had the DDS return the CSV as a B64 encoded string which I bound to a field data.rawData.  On click of a button I then downloaded it using the code below.  This seems to work well on IE and Chrome.

      DDS returned something like this (snipped)

      {"response":{"csv":"QXBwbGljYXRpb25JZCxTd...93LCwsDQo="}}


      Click event on button:

          function requiredFeaturesSupported() {
            return ( BlobConstructor() && msSaveOrOpenBlobSupported() );
          }
          
          function BlobConstructor() {
            if (!window.Blob) {
              //document.getElementsByTagName('body')[0].innerHTML = "<h1>The Blob constructor is not supported - upgrade your browser and try again.</h1>";
              return false;
            } // if
            
            return true;
          } // BlobConstructor
          
          function msSaveOrOpenBlobSupported() {
            if (!window.navigator.msSaveOrOpenBlob) { // If msSaveOrOpenBlob() is supported, then so is msSaveBlob().
              //document.getElementsByTagName('body')[0].innerHTML = "<h1>The msSaveOrOpenBlob API is not supported - try upgrading your version of IE to the latest version.</h1>";            
              return false;
            } // if
            
            return true;
          } // msSaveOrOpenBlobSupported
      
          function b64toBlob(b64Data, contentType, sliceSize) {
            contentType = contentType || '';
            sliceSize = sliceSize || 512;
      
            var byteCharacters = atob(b64Data);
            var byteArrays = [];
      
            for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) {
              var slice = byteCharacters.slice(offset, offset + sliceSize);
      
              var byteNumbers = new Array(slice.length);
              for (var i = 0; i < slice.length; i++) {
                byteNumbers[i] = slice.charCodeAt(i);
              }
      
              var byteArray = new Uint8Array(byteNumbers);
      
              byteArrays.push(byteArray);
            }
      
            var blob = new Blob(byteArrays, {type: contentType});
            return blob;
          }
          if (requiredFeaturesSupported()) {
             
            blobObject = b64toBlob(JSON.parse(JSON.stringify(data.rawdata)).csv,"text/csv")
            window.navigator.msSaveOrOpenBlob(blobObject, 'report.csv');
       
          }else{
      
      	    var a = document.body.appendChild(
                  document.createElement("a")
              );
              a.download = "report.csv";
              a.href = "data:text/csv;charset=utf-8;base64," + JSON.parse(JSON.stringify(data.rawdata)).csv;
              a.innerHTML = "download report csv";
            	a.hidden = true;
              a.click();
          }
      
      
      



      1. Ankit Chauhan

        Thanks Robert :).

      CommentAdd your comment...