title: Force Javascript to convert stream into file returned from server
body: <p>I am making a call to server, which returns me a file (excel or pdf) but instead of downloading the file - it's failing. I just want to download the file when user clicks on the button. I tried following approaches,</p>
  
  <p><strong>Approach 1:</strong>
  Added a button and then click event on it</p>
  
  <pre><code>$("#myButton").on("click", function () {
      var url = "www.getfile.com/GetFile.svc/files/?Id=" + GetID() + "Get&amp;format=xlsx";
      $.getJSON(url)
      .done(function (data) {
          console.log(data);
      })
      .fail(function () {
          console.log("Failed");
      })
  });
  </code></pre>
  
  <p><strong>Approach 2:</strong>
  Added a anchor tag and then changing it's href (which I need to to get some ID)</p>
  
  <pre><code>   $("#myAnchor").on("click", function () {
                 var url = "www.getfile.com/GetFile.svc/files/?Id=" + GetID() + "Get&amp;format=xlsx";
          this.href = url;
          return false;
      });
  </code></pre>
  
  <p>1st approach just shows "Failed" in console and 2nd approach does nothing at all..</p>
  
  <p><a href="https://i.stack.imgur.com/TrOiC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/TrOiC.png" alt="enter image description here"></a></p>
  
  <p>I am getting data from the services and <strong>IF I go to the web services URL in browser it just downloads the file</strong></p>
  
  <p>My requirements are to download the file after clicking on it without any redirects involved.</p>