sugarcrm fileDownload error after upgrade

I received this error after I upgraded sugar7.8, Which calling my filedownload.

{"error":"need_login","error_message":"No valid authentication for user."}

After some Investigation found tht sugar upgraded the API calls for OAuth. Following is my CODE:

 api.fileDownload(api.buildURL("Quotes/" + model.get("id") + "/pdf/download?OAuth-Token=" + api.getOAuthToken()), {        success: function() {            app.alert.show("pdf_download_api_success", {                level: "success",                messages: SUGAR.language.get('Quotes', 'LBL_QUOTE_PDF_GENERATED'),                autoClose: true            });        },});

I checked the detials in the Following url: But I could not able to add headder to the HTTPS request can some one help?

https://developer.sugarcrm.com/2016/11/15/security-changes-coming-in-sugar-7-8/

  • After so much research, I came up with a solution for this issue.

    Note: There is no supporting document for `api.fileDownload(` to use `OAuth-token`.

    So i tried using `XMLHttpRequest` and it worked fine.

    **SOLUTION**

    var request = new XMLHttpRequest();
    request.open('GET', api.buildURL("YOURMODULE/" + model.get("id") + "/pdf/download"), true);
    request.setRequestHeader('OAuth-Token', api.getOAuthToken()); // UR TOKEN
    request.responseType = "blob";
    request.onload = function (e) {
    if (this.status === 200) {
          // `blob` response
          // create `objectURL` of `this.response` : `.pdf` as `Blob`
          var file = window.URL.createObjectURL(this.response);
          var a = document.createElement("a");
          a.href = file;
          /*request.onreadystatechange = function() {
                if(this.readyState == this.HEADERS_RECEIVED) {
                      console.log(request.getResponseHeader("Content-Type"));
                }
          }*/

          a.download = request.getResponseHeader("FileName");
          document.body.appendChild(a);
          a.click();
          document.body.removeChild(a);
       };
    };
    request.send();