title: angular 6 download excel from API
body: <p>I am trying to download an excel file from server. UI is in angular 6 and service is C# Web API. Firstly, I am not getting whether this download method need to be HTTPGET or HTTPPOST. Using some help from other forum, I have written code below. I don't see any error nor I see that debugger stop inside subscribe method. when I click on file link to download from angular app the page redirect to localhost: port (start page)</p>
  
  <pre><code>     [HttpGet]//http get as it return file 
          public HttpResponseMessage DownloadAttachment(string fileName)
          {
              //below code locate physical file on server 
              var localFilePath = HttpContext.Current.Server.MapPath("../../uploadFiles/" + fileName);
              HttpResponseMessage response = null;
              if (!File.Exists(localFilePath))
              {
                  //if file not found than return response as resource not present 
                  response = Request.CreateResponse(HttpStatusCode.Gone);
              }
              else
              {
                  //if file present than read file 
                  var fStream = new FileStream(localFilePath, FileMode.Open, FileAccess.Read);
  
                 //compose response and include file as content in it
                  response = new HttpResponseMessage
                  {
                      StatusCode = HttpStatusCode.OK,
                      // Content = new StreamContent(fStream)
                      Content = new StreamContent(fStream)
                  };
  
                  //set content header of reponse as file attached in reponse
                  response.Content.Headers.ContentDisposition =
                  new ContentDispositionHeaderValue("attachment")
                  {
                      FileName = Path.GetFileName(fStream.Name)
                  };
                  //set the content header content type as application/octet-stream as it      
                  //returning file as reponse 
                 response.Content.Headers.ContentType = new
                               MediaTypeHeaderValue("application/octet-stream");
  
  
                  response.Content.Headers.ContentLength = fStream.Length;
                  response.Headers.Add("fileName", fileName);
              }
              return response;
          }
  </code></pre>
  
  <p>Now, from Angular calling:</p>
  
  <pre><code>   downloadFile(fileName: string) {
    this.Service.postAndGetResponse(fileName).subscribe(fileData =&gt; {
     // const b: any = new Blob([fileData], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
      // .map(res =&gt; new Blob([res.blob()],{ type: 'application/vnd.ms-excel' }));
     // const  url = window.URL.createObjectURL(b);
     //   window.open(url);
     console.log (fileData);
      }
    );
  }
  </code></pre>
  
  <p>in Service.ts</p>
  
  <pre><code>     postAndGetResponse(fileName) {
      return this.http.get(this.baseURL + 'DataEvent/DownloadEventAttachment?fileName=' + fileName, {responseType: 'blob' as 'blob'}).pipe(
        map((x) =&gt; {
          return x;
      })
      );
    }
  </code></pre>
  
  <p>I have put debugger in downloadFile method inside subscribe, but it never stops there as if nothing is returned or call is lost.</p>
  
  <p>When I use postman to call WEB API method, the response is returned. I don't see any text format- it seems to be corrupted/binary?? the format is like below in response body:</p>
  
  <pre><code>    ��ࡱ�&gt;��   �����������������
  </code></pre>