downloading a docx file from ajaxResponse











up vote
3
down vote

favorite












So I tried to download a docx document as a report . by converting ajax response to a blob then an url ! the result is a document word that display a message , " we're sorry . we can't open because we found a problem with it's content .
there the main :
onDownloadReport: function (oEvent) {



var oAjaxBody = {
SessionId: this.getModel("sessionModel").getData().SessionId,
CustomerName: encodeURIComponent(this.getModel("sessionModel").getData().CustomerName.split("n")[0]),
TenantInfo: encodeURIComponent(this.getModel("sessionModel").getData().TenantInfo)
};
var sServiceUrl = "/SDC_XS_TEMP/APPL/SDC/services/serviceRuntime/xsjs/SDC_REPORT_GENERATE_MM.xsjs";

var me = this;
$.ajax({
url: sServiceUrl,
type: "POST",
responseType:'arraybuffer',
contentType: "application/json",
data: JSON.stringify(oAjaxBody),
// dataType: "json",
success: function (oAjaxResponse) {
var content = oAjaxResponse;
var fileName = 'rapport.docx'; // You can use the .txt extension if you want
me.downloadwithpost(fileName, content);
},
error: function (oError) {
console.log("failure");
}

});


and this is the function



downloadwithpost: function (filename, content) {
var link = document.createElement('a');
var bytes = new Array(content.length);
// var bytes = new Array(content.length);
for (var i = 0; i < content.length; i++) {
bytes[i] = content.charCodeAt(i);
}
var byteArray = new Uint8Array(bytes);
var blob = new Blob([byteArray], {
type: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
});
var url = URL.createObjectURL(blob);
var oItem = {
documentId: url,
fileName: "rapport.docx",
thumbnailUrl: "",
url: url,
selected: true
};
var oUploadCollection = this.getView().byId("uploadCollection");
var newItem = new sap.m.UploadCollectionItem(oItem);
oUploadCollection.addItem(newItem);
oUploadCollection.downloadItem(newItem, true);
}









share|improve this question









New contributor




Alexis sanchez is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    up vote
    3
    down vote

    favorite












    So I tried to download a docx document as a report . by converting ajax response to a blob then an url ! the result is a document word that display a message , " we're sorry . we can't open because we found a problem with it's content .
    there the main :
    onDownloadReport: function (oEvent) {



    var oAjaxBody = {
    SessionId: this.getModel("sessionModel").getData().SessionId,
    CustomerName: encodeURIComponent(this.getModel("sessionModel").getData().CustomerName.split("n")[0]),
    TenantInfo: encodeURIComponent(this.getModel("sessionModel").getData().TenantInfo)
    };
    var sServiceUrl = "/SDC_XS_TEMP/APPL/SDC/services/serviceRuntime/xsjs/SDC_REPORT_GENERATE_MM.xsjs";

    var me = this;
    $.ajax({
    url: sServiceUrl,
    type: "POST",
    responseType:'arraybuffer',
    contentType: "application/json",
    data: JSON.stringify(oAjaxBody),
    // dataType: "json",
    success: function (oAjaxResponse) {
    var content = oAjaxResponse;
    var fileName = 'rapport.docx'; // You can use the .txt extension if you want
    me.downloadwithpost(fileName, content);
    },
    error: function (oError) {
    console.log("failure");
    }

    });


    and this is the function



    downloadwithpost: function (filename, content) {
    var link = document.createElement('a');
    var bytes = new Array(content.length);
    // var bytes = new Array(content.length);
    for (var i = 0; i < content.length; i++) {
    bytes[i] = content.charCodeAt(i);
    }
    var byteArray = new Uint8Array(bytes);
    var blob = new Blob([byteArray], {
    type: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
    });
    var url = URL.createObjectURL(blob);
    var oItem = {
    documentId: url,
    fileName: "rapport.docx",
    thumbnailUrl: "",
    url: url,
    selected: true
    };
    var oUploadCollection = this.getView().byId("uploadCollection");
    var newItem = new sap.m.UploadCollectionItem(oItem);
    oUploadCollection.addItem(newItem);
    oUploadCollection.downloadItem(newItem, true);
    }









    share|improve this question









    New contributor




    Alexis sanchez is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      3
      down vote

      favorite









      up vote
      3
      down vote

      favorite











      So I tried to download a docx document as a report . by converting ajax response to a blob then an url ! the result is a document word that display a message , " we're sorry . we can't open because we found a problem with it's content .
      there the main :
      onDownloadReport: function (oEvent) {



      var oAjaxBody = {
      SessionId: this.getModel("sessionModel").getData().SessionId,
      CustomerName: encodeURIComponent(this.getModel("sessionModel").getData().CustomerName.split("n")[0]),
      TenantInfo: encodeURIComponent(this.getModel("sessionModel").getData().TenantInfo)
      };
      var sServiceUrl = "/SDC_XS_TEMP/APPL/SDC/services/serviceRuntime/xsjs/SDC_REPORT_GENERATE_MM.xsjs";

      var me = this;
      $.ajax({
      url: sServiceUrl,
      type: "POST",
      responseType:'arraybuffer',
      contentType: "application/json",
      data: JSON.stringify(oAjaxBody),
      // dataType: "json",
      success: function (oAjaxResponse) {
      var content = oAjaxResponse;
      var fileName = 'rapport.docx'; // You can use the .txt extension if you want
      me.downloadwithpost(fileName, content);
      },
      error: function (oError) {
      console.log("failure");
      }

      });


      and this is the function



      downloadwithpost: function (filename, content) {
      var link = document.createElement('a');
      var bytes = new Array(content.length);
      // var bytes = new Array(content.length);
      for (var i = 0; i < content.length; i++) {
      bytes[i] = content.charCodeAt(i);
      }
      var byteArray = new Uint8Array(bytes);
      var blob = new Blob([byteArray], {
      type: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
      });
      var url = URL.createObjectURL(blob);
      var oItem = {
      documentId: url,
      fileName: "rapport.docx",
      thumbnailUrl: "",
      url: url,
      selected: true
      };
      var oUploadCollection = this.getView().byId("uploadCollection");
      var newItem = new sap.m.UploadCollectionItem(oItem);
      oUploadCollection.addItem(newItem);
      oUploadCollection.downloadItem(newItem, true);
      }









      share|improve this question









      New contributor




      Alexis sanchez is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      So I tried to download a docx document as a report . by converting ajax response to a blob then an url ! the result is a document word that display a message , " we're sorry . we can't open because we found a problem with it's content .
      there the main :
      onDownloadReport: function (oEvent) {



      var oAjaxBody = {
      SessionId: this.getModel("sessionModel").getData().SessionId,
      CustomerName: encodeURIComponent(this.getModel("sessionModel").getData().CustomerName.split("n")[0]),
      TenantInfo: encodeURIComponent(this.getModel("sessionModel").getData().TenantInfo)
      };
      var sServiceUrl = "/SDC_XS_TEMP/APPL/SDC/services/serviceRuntime/xsjs/SDC_REPORT_GENERATE_MM.xsjs";

      var me = this;
      $.ajax({
      url: sServiceUrl,
      type: "POST",
      responseType:'arraybuffer',
      contentType: "application/json",
      data: JSON.stringify(oAjaxBody),
      // dataType: "json",
      success: function (oAjaxResponse) {
      var content = oAjaxResponse;
      var fileName = 'rapport.docx'; // You can use the .txt extension if you want
      me.downloadwithpost(fileName, content);
      },
      error: function (oError) {
      console.log("failure");
      }

      });


      and this is the function



      downloadwithpost: function (filename, content) {
      var link = document.createElement('a');
      var bytes = new Array(content.length);
      // var bytes = new Array(content.length);
      for (var i = 0; i < content.length; i++) {
      bytes[i] = content.charCodeAt(i);
      }
      var byteArray = new Uint8Array(bytes);
      var blob = new Blob([byteArray], {
      type: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
      });
      var url = URL.createObjectURL(blob);
      var oItem = {
      documentId: url,
      fileName: "rapport.docx",
      thumbnailUrl: "",
      url: url,
      selected: true
      };
      var oUploadCollection = this.getView().byId("uploadCollection");
      var newItem = new sap.m.UploadCollectionItem(oItem);
      oUploadCollection.addItem(newItem);
      oUploadCollection.downloadItem(newItem, true);
      }






      javascript ajax sapui5






      share|improve this question









      New contributor




      Alexis sanchez is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      Alexis sanchez is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 22 hours ago









      Eugene Mihaylin

      808224




      808224






      New contributor




      Alexis sanchez is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 23 hours ago









      Alexis sanchez

      161




      161




      New contributor




      Alexis sanchez is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Alexis sanchez is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Alexis sanchez is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote













          Take a look at this example, I worked with an hexadecimal data format, but you can change it as you see fit. Let me know if you need any help with this example or have any questions.



             doAfterSuccess: function(result, fileName, fileType) {       
          var filedata = result.ARRAY[0].FILEDATA; // get data from response
          // ------------------------------- hex data, rapport, docx
          var createdFile = this.createFile(filedata, fileName, fileType);
          this.downloadFile(createdFile, fileName, fileType);
          },

          createFile: function(hexContent, filename, type) {
          var data = this.convertHexToBinary(hexContent); // skip if your data is not hex
          var file = new Blob([data], {
          name: filename,
          type: type
          });
          return file;
          },

          convertHexToBinary: function(hexContent) {
          return new Uint8Array(hexContent.match(/.{2}/g).map(function(e)
          {
          return parseInt(e, 16);
          }));
          },
          downloadFile: function(file, filename, type){
          if (window.navigator.msSaveOrOpenBlob)
          window.navigator.msSaveOrOpenBlob(file, filename);
          else { // Others
          var a = document.createElement("a"),
          url = URL.createObjectURL(file);
          a.href = url;
          a.download = filename + "." + type;
          document.body.appendChild(a);
          a.click();
          setTimeout(function() {
          document.body.removeChild(a);
          window.URL.revokeObjectURL(url);
          }, 0);
          }
          },





          share|improve this answer




























            up vote
            0
            down vote













            function downloadDoc(filename, sServiceUrl, oAjaxBody){
            var xhr = new XMLHttpRequest();
            xhr.open('POST', sServiceUrl, true);
            xhr.responseType = 'blob';
            xhr.send(JSON.stringify(oAjaxBody));
            xhr.onreadystatechange = function(){
            if (xhr.readyState == 4){
            //$.unblockUI();
            if(xhr.status == 200) {
            var blob = new Blob([xhr.response], { type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' });
            if (navigator.msSaveOrOpenBlob)
            navigator.msSaveOrOpenBlob(blob, filename);
            else {
            var link = document.createElement('a');
            var URL = window.URL || window.webkitURL;
            var downloadUrl = URL.createObjectURL(blob);
            link.href = downloadUrl;
            link.style = "display: none";
            link.download = filename;
            document.body.appendChild(link);
            link.click();
            setTimeout(function(){
            document.body.removeChild(link);
            window.URL.revokeObjectURL(downloadUrl);
            }, 100);
            }
            }
            else {
            console.log("failure");
            }
            }
            }
            }





            share|improve this answer























            • so as params , we don't have an url , just an oAjaxResponse .
              – Alexis sanchez
              23 hours ago










            • the major problem is to convert an oajax reponse to blob
              – Alexis sanchez
              22 hours ago










            • Why use XMLHttpRequest? Please refer to stackoverflow.com/questions/17657184/…
              – Miller Cy Chan
              1 hour ago











            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });






            Alexis sanchez is a new contributor. Be nice, and check out our Code of Conduct.










             

            draft saved


            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53372174%2fdownloading-a-docx-file-from-ajaxresponse%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            Take a look at this example, I worked with an hexadecimal data format, but you can change it as you see fit. Let me know if you need any help with this example or have any questions.



               doAfterSuccess: function(result, fileName, fileType) {       
            var filedata = result.ARRAY[0].FILEDATA; // get data from response
            // ------------------------------- hex data, rapport, docx
            var createdFile = this.createFile(filedata, fileName, fileType);
            this.downloadFile(createdFile, fileName, fileType);
            },

            createFile: function(hexContent, filename, type) {
            var data = this.convertHexToBinary(hexContent); // skip if your data is not hex
            var file = new Blob([data], {
            name: filename,
            type: type
            });
            return file;
            },

            convertHexToBinary: function(hexContent) {
            return new Uint8Array(hexContent.match(/.{2}/g).map(function(e)
            {
            return parseInt(e, 16);
            }));
            },
            downloadFile: function(file, filename, type){
            if (window.navigator.msSaveOrOpenBlob)
            window.navigator.msSaveOrOpenBlob(file, filename);
            else { // Others
            var a = document.createElement("a"),
            url = URL.createObjectURL(file);
            a.href = url;
            a.download = filename + "." + type;
            document.body.appendChild(a);
            a.click();
            setTimeout(function() {
            document.body.removeChild(a);
            window.URL.revokeObjectURL(url);
            }, 0);
            }
            },





            share|improve this answer

























              up vote
              0
              down vote













              Take a look at this example, I worked with an hexadecimal data format, but you can change it as you see fit. Let me know if you need any help with this example or have any questions.



                 doAfterSuccess: function(result, fileName, fileType) {       
              var filedata = result.ARRAY[0].FILEDATA; // get data from response
              // ------------------------------- hex data, rapport, docx
              var createdFile = this.createFile(filedata, fileName, fileType);
              this.downloadFile(createdFile, fileName, fileType);
              },

              createFile: function(hexContent, filename, type) {
              var data = this.convertHexToBinary(hexContent); // skip if your data is not hex
              var file = new Blob([data], {
              name: filename,
              type: type
              });
              return file;
              },

              convertHexToBinary: function(hexContent) {
              return new Uint8Array(hexContent.match(/.{2}/g).map(function(e)
              {
              return parseInt(e, 16);
              }));
              },
              downloadFile: function(file, filename, type){
              if (window.navigator.msSaveOrOpenBlob)
              window.navigator.msSaveOrOpenBlob(file, filename);
              else { // Others
              var a = document.createElement("a"),
              url = URL.createObjectURL(file);
              a.href = url;
              a.download = filename + "." + type;
              document.body.appendChild(a);
              a.click();
              setTimeout(function() {
              document.body.removeChild(a);
              window.URL.revokeObjectURL(url);
              }, 0);
              }
              },





              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                Take a look at this example, I worked with an hexadecimal data format, but you can change it as you see fit. Let me know if you need any help with this example or have any questions.



                   doAfterSuccess: function(result, fileName, fileType) {       
                var filedata = result.ARRAY[0].FILEDATA; // get data from response
                // ------------------------------- hex data, rapport, docx
                var createdFile = this.createFile(filedata, fileName, fileType);
                this.downloadFile(createdFile, fileName, fileType);
                },

                createFile: function(hexContent, filename, type) {
                var data = this.convertHexToBinary(hexContent); // skip if your data is not hex
                var file = new Blob([data], {
                name: filename,
                type: type
                });
                return file;
                },

                convertHexToBinary: function(hexContent) {
                return new Uint8Array(hexContent.match(/.{2}/g).map(function(e)
                {
                return parseInt(e, 16);
                }));
                },
                downloadFile: function(file, filename, type){
                if (window.navigator.msSaveOrOpenBlob)
                window.navigator.msSaveOrOpenBlob(file, filename);
                else { // Others
                var a = document.createElement("a"),
                url = URL.createObjectURL(file);
                a.href = url;
                a.download = filename + "." + type;
                document.body.appendChild(a);
                a.click();
                setTimeout(function() {
                document.body.removeChild(a);
                window.URL.revokeObjectURL(url);
                }, 0);
                }
                },





                share|improve this answer












                Take a look at this example, I worked with an hexadecimal data format, but you can change it as you see fit. Let me know if you need any help with this example or have any questions.



                   doAfterSuccess: function(result, fileName, fileType) {       
                var filedata = result.ARRAY[0].FILEDATA; // get data from response
                // ------------------------------- hex data, rapport, docx
                var createdFile = this.createFile(filedata, fileName, fileType);
                this.downloadFile(createdFile, fileName, fileType);
                },

                createFile: function(hexContent, filename, type) {
                var data = this.convertHexToBinary(hexContent); // skip if your data is not hex
                var file = new Blob([data], {
                name: filename,
                type: type
                });
                return file;
                },

                convertHexToBinary: function(hexContent) {
                return new Uint8Array(hexContent.match(/.{2}/g).map(function(e)
                {
                return parseInt(e, 16);
                }));
                },
                downloadFile: function(file, filename, type){
                if (window.navigator.msSaveOrOpenBlob)
                window.navigator.msSaveOrOpenBlob(file, filename);
                else { // Others
                var a = document.createElement("a"),
                url = URL.createObjectURL(file);
                a.href = url;
                a.download = filename + "." + type;
                document.body.appendChild(a);
                a.click();
                setTimeout(function() {
                document.body.removeChild(a);
                window.URL.revokeObjectURL(url);
                }, 0);
                }
                },






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 2 hours ago









                Matthijs Mennens

                1,035216




                1,035216
























                    up vote
                    0
                    down vote













                    function downloadDoc(filename, sServiceUrl, oAjaxBody){
                    var xhr = new XMLHttpRequest();
                    xhr.open('POST', sServiceUrl, true);
                    xhr.responseType = 'blob';
                    xhr.send(JSON.stringify(oAjaxBody));
                    xhr.onreadystatechange = function(){
                    if (xhr.readyState == 4){
                    //$.unblockUI();
                    if(xhr.status == 200) {
                    var blob = new Blob([xhr.response], { type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' });
                    if (navigator.msSaveOrOpenBlob)
                    navigator.msSaveOrOpenBlob(blob, filename);
                    else {
                    var link = document.createElement('a');
                    var URL = window.URL || window.webkitURL;
                    var downloadUrl = URL.createObjectURL(blob);
                    link.href = downloadUrl;
                    link.style = "display: none";
                    link.download = filename;
                    document.body.appendChild(link);
                    link.click();
                    setTimeout(function(){
                    document.body.removeChild(link);
                    window.URL.revokeObjectURL(downloadUrl);
                    }, 100);
                    }
                    }
                    else {
                    console.log("failure");
                    }
                    }
                    }
                    }





                    share|improve this answer























                    • so as params , we don't have an url , just an oAjaxResponse .
                      – Alexis sanchez
                      23 hours ago










                    • the major problem is to convert an oajax reponse to blob
                      – Alexis sanchez
                      22 hours ago










                    • Why use XMLHttpRequest? Please refer to stackoverflow.com/questions/17657184/…
                      – Miller Cy Chan
                      1 hour ago















                    up vote
                    0
                    down vote













                    function downloadDoc(filename, sServiceUrl, oAjaxBody){
                    var xhr = new XMLHttpRequest();
                    xhr.open('POST', sServiceUrl, true);
                    xhr.responseType = 'blob';
                    xhr.send(JSON.stringify(oAjaxBody));
                    xhr.onreadystatechange = function(){
                    if (xhr.readyState == 4){
                    //$.unblockUI();
                    if(xhr.status == 200) {
                    var blob = new Blob([xhr.response], { type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' });
                    if (navigator.msSaveOrOpenBlob)
                    navigator.msSaveOrOpenBlob(blob, filename);
                    else {
                    var link = document.createElement('a');
                    var URL = window.URL || window.webkitURL;
                    var downloadUrl = URL.createObjectURL(blob);
                    link.href = downloadUrl;
                    link.style = "display: none";
                    link.download = filename;
                    document.body.appendChild(link);
                    link.click();
                    setTimeout(function(){
                    document.body.removeChild(link);
                    window.URL.revokeObjectURL(downloadUrl);
                    }, 100);
                    }
                    }
                    else {
                    console.log("failure");
                    }
                    }
                    }
                    }





                    share|improve this answer























                    • so as params , we don't have an url , just an oAjaxResponse .
                      – Alexis sanchez
                      23 hours ago










                    • the major problem is to convert an oajax reponse to blob
                      – Alexis sanchez
                      22 hours ago










                    • Why use XMLHttpRequest? Please refer to stackoverflow.com/questions/17657184/…
                      – Miller Cy Chan
                      1 hour ago













                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    function downloadDoc(filename, sServiceUrl, oAjaxBody){
                    var xhr = new XMLHttpRequest();
                    xhr.open('POST', sServiceUrl, true);
                    xhr.responseType = 'blob';
                    xhr.send(JSON.stringify(oAjaxBody));
                    xhr.onreadystatechange = function(){
                    if (xhr.readyState == 4){
                    //$.unblockUI();
                    if(xhr.status == 200) {
                    var blob = new Blob([xhr.response], { type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' });
                    if (navigator.msSaveOrOpenBlob)
                    navigator.msSaveOrOpenBlob(blob, filename);
                    else {
                    var link = document.createElement('a');
                    var URL = window.URL || window.webkitURL;
                    var downloadUrl = URL.createObjectURL(blob);
                    link.href = downloadUrl;
                    link.style = "display: none";
                    link.download = filename;
                    document.body.appendChild(link);
                    link.click();
                    setTimeout(function(){
                    document.body.removeChild(link);
                    window.URL.revokeObjectURL(downloadUrl);
                    }, 100);
                    }
                    }
                    else {
                    console.log("failure");
                    }
                    }
                    }
                    }





                    share|improve this answer














                    function downloadDoc(filename, sServiceUrl, oAjaxBody){
                    var xhr = new XMLHttpRequest();
                    xhr.open('POST', sServiceUrl, true);
                    xhr.responseType = 'blob';
                    xhr.send(JSON.stringify(oAjaxBody));
                    xhr.onreadystatechange = function(){
                    if (xhr.readyState == 4){
                    //$.unblockUI();
                    if(xhr.status == 200) {
                    var blob = new Blob([xhr.response], { type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' });
                    if (navigator.msSaveOrOpenBlob)
                    navigator.msSaveOrOpenBlob(blob, filename);
                    else {
                    var link = document.createElement('a');
                    var URL = window.URL || window.webkitURL;
                    var downloadUrl = URL.createObjectURL(blob);
                    link.href = downloadUrl;
                    link.style = "display: none";
                    link.download = filename;
                    document.body.appendChild(link);
                    link.click();
                    setTimeout(function(){
                    document.body.removeChild(link);
                    window.URL.revokeObjectURL(downloadUrl);
                    }, 100);
                    }
                    }
                    else {
                    console.log("failure");
                    }
                    }
                    }
                    }






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited 1 hour ago

























                    answered 23 hours ago









                    Miller Cy Chan

                    1569




                    1569












                    • so as params , we don't have an url , just an oAjaxResponse .
                      – Alexis sanchez
                      23 hours ago










                    • the major problem is to convert an oajax reponse to blob
                      – Alexis sanchez
                      22 hours ago










                    • Why use XMLHttpRequest? Please refer to stackoverflow.com/questions/17657184/…
                      – Miller Cy Chan
                      1 hour ago


















                    • so as params , we don't have an url , just an oAjaxResponse .
                      – Alexis sanchez
                      23 hours ago










                    • the major problem is to convert an oajax reponse to blob
                      – Alexis sanchez
                      22 hours ago










                    • Why use XMLHttpRequest? Please refer to stackoverflow.com/questions/17657184/…
                      – Miller Cy Chan
                      1 hour ago
















                    so as params , we don't have an url , just an oAjaxResponse .
                    – Alexis sanchez
                    23 hours ago




                    so as params , we don't have an url , just an oAjaxResponse .
                    – Alexis sanchez
                    23 hours ago












                    the major problem is to convert an oajax reponse to blob
                    – Alexis sanchez
                    22 hours ago




                    the major problem is to convert an oajax reponse to blob
                    – Alexis sanchez
                    22 hours ago












                    Why use XMLHttpRequest? Please refer to stackoverflow.com/questions/17657184/…
                    – Miller Cy Chan
                    1 hour ago




                    Why use XMLHttpRequest? Please refer to stackoverflow.com/questions/17657184/…
                    – Miller Cy Chan
                    1 hour ago










                    Alexis sanchez is a new contributor. Be nice, and check out our Code of Conduct.










                     

                    draft saved


                    draft discarded


















                    Alexis sanchez is a new contributor. Be nice, and check out our Code of Conduct.













                    Alexis sanchez is a new contributor. Be nice, and check out our Code of Conduct.












                    Alexis sanchez is a new contributor. Be nice, and check out our Code of Conduct.















                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53372174%2fdownloading-a-docx-file-from-ajaxresponse%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

                    SQL update select statement

                    'app-layout' is not a known element: how to share Component with different Modules