title: Downloading files with Angular 2 and .Net Core API
body: <p>I have a WebAPI that has a method which makes an Excel file and returns it using <code>File()</code>. </p>
  
  <p>In my Angular app I subscribe to the response and download the file using blob. But I am getting an error in the browser console which says it can't parse the response to a JSON.  </p>
  
  <p>Why does it want to parse the response to a JSON? And how to fix this problem?</p>
  
  <p>API Controller: </p>
  
  <pre><code> [HttpPost]
   public IActionResult QRGenerator(QRCode qrCode)
   {
        string wwwrootPath = _hostingEnvironment.WebRootPath;
  
         var QrCodes = new List&lt;QRCode&gt;();
         QrCodes.Add(qrCode);
         ExcelGenerator generator = new ExcelGenerator();
         var fileName = generator.GeneratePrintableSheets(QrCodes, wwwrootPath);
         var path = Path.Combine(wwwrootPath, fileName);
         return DownloadFile(fileName, wwwrootPath);   
    }
  
    public IActionResult DownloadFile(string fileName, string wwwrootPath)
    {
        var path = Path.Combine(wwwrootPath, fileName);
        var content = System.IO.File.ReadAllBytes(path);
        return File(content, "application/vnd.ms-excel", fileName);
     }
  </code></pre>
  
  <p>Angular Component:</p>
  
  <pre><code>  createQR(qrCode: QRCode) {
      let formData: FormData = new FormData();
      this.singleQr.CreateSingleQR(formData).subscribe(data =&gt; {
        this.downloadFile(data);
        console.log(data);
      });
    }
  
   downloadFile(data: any){
     var blob = new Blob([data], { type: 'application/vnd.ms-excel' });
     var url= window.URL.createObjectURL(blob);
     window.open(url);
   }
  </code></pre>
  
  <p>And for completion sake the Angular Service:</p>
  
  <pre><code> CreateSingleQR(formdata: FormData) {
      return this.http.post(this.qrUrl, formdata);
    }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/przGX.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/przGX.png" alt="Browser Error"></a></p>