0

I have different files on my server and I try to download some of them. It has different names and extensions and I can't hardcode them in my code.

How I can download such files?

I'm using .Net core web-api and Angular 7.

.Net:

public async Task<IActionResult> GetFile(DownloadFileQuery query)
{            
    try
    {                
        var result = await QueryHandler.Handle(query);

        return File(result.Content, "application/octet-stream", result.Name);
    }
    catch (Exception e)
    {
        return BadRequest(e);
    }
}

Angular:

// component
// my console log:
// Blob {size: 103550, type: "application/octet-stream"}
// I don't receive filename and extension
..
downloadFile(fileId: string) {
    this.fileService.downloadFile(fileId).subscribe(
      data => {
        console.log(data);
        FileSaver(data, 'How can I find out which file I received?');
      },
      error => console.log(error)
    );
  }
..
// service
..
downloadFile(fileId: string) {
    const queryParams = new HttpParams()
                .set('FileId', param);
    return this.http.get(`api/file/file`, { params: queryParams , responseType: 'blob'});
  }
..

I expect to get filename.extension in response. Or another way to do this.

Kirk Larkin
  • 84,915
  • 16
  • 214
  • 203
IngBond
  • 601
  • 1
  • 5
  • 18
  • 2
    Your ASP.NET Core controller sets the [`Content-Disposition`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition) header, which includes a `filename` value that you can read out. – Kirk Larkin May 24 '19 at 15:39
  • 1
    This could help: https://stackoverflow.com/questions/45505619/angular-4-3-3-httpclient-how-get-value-from-the-header-of-a-response – Christoph Lütjen May 24 '19 at 15:41
  • Thanxdfsdfsdfsdf – IngBond May 27 '19 at 14:02

0 Answers0