2

I am working on an Angular 7 and .NET Core application. I have to pass file (file contents) from .NET API to Angular. My API code is:

public async Task<IActionResult> GetLicenseInformation()
{
    try
    {
        string fileName = "TestLicense";

        string filePath = PathOfFile;

        var bytearray = System.IO.File.ReadAllBytes(pathToSaveLicenseFile);
        const string contentType = "text/plain";
        var result = new FileContentResult(bytearray, contentType)
        {
            FileDownloadName = fileName
        };

        return result;
    }
    catch (Exception ex)
    {
    
    }
    return null;
}

My service code on Angular side is:

getLicenseInformation(): Observable<any> {

return this.http.get(BaseUrl + 'getlicenseinformation',  {responseType: 'blob' , observe: 'response'});
}

My code of component is:

ngOnInit() {

this.uploadService.getLicenseInformation().subscribe(data => {
  var blob = new Blob([data.blob()], {type: 'text/plain'});
  })
}

When i run the code, i get the exception data.blob is not a function.

I have also tried data.body(), this does not give exception but i dont get the data of the file. I have tried this API with postman and its working fine i.e. i do get the file contents in postman. What may be the issue ? What am i doing wrong ?

Waleed Naveed
  • 2,293
  • 2
  • 29
  • 57

1 Answers1