0

here is what is happening

  • If I copy paste my endpoint URL(https://localhost:44348/api/TestController1) in the browse and press enter then it is working, file will be downloaded
  • but if I press the button then it is not working

Below are my code current snippets

Api Controller Code:

// GET: api/<TestController1>
    [HttpGet]
    public ActionResult Get()
    {
        JsonObject jsonObject = new JsonObject()
        {
            Name = "Keval Chauhan",
            EnrollmentNumber = 1234567890
        };

        var serializedObject = JsonConvert.SerializeObject(jsonObject, new JsonSerializerSettings());
        byte[] bytes = Encoding.UTF8.GetBytes(serializedObject);

        var stream = new MemoryStream(bytes);

        var result = new FileStreamResult(stream, "text/json");
        result.FileDownloadName = "test.json";
        return result;
    }

Blazor Page Code that is executed on button press:

public async void DownloadJSON()
{
    HttpClient httpClient = new HttpClient();
    await httpClient.GetAsync("https://localhost:44348/api/TestController1");
}
  • 1
    It looks to me that in `DownloadJson` you're getting the FileStreamResult, but then not doing anything with it, as the method is `void`? – stuartd Oct 21 '20 at 13:12
  • When you press the button is it making the call but not downloading? Or it's not the call at all? Place a breakpoint in your controller and see it it hits – David Edel Oct 21 '20 at 13:14
  • 1
    *don't* use `async void`. That was only meant for event handlers. Not even event callbacks. Use `async Task` for asynchronous methods that don't return anything. The code you wrote doesn't do anything with the response anyway. – Panagiotis Kanavos Oct 21 '20 at 13:34
  • Are you wanting to consume the data or simply download it as a file? – Brian Parker Oct 21 '20 at 13:44
  • Hey! @DavidEdel thank you for commenting, it is making a call but not downloading. – Keval Chauhan Oct 22 '20 at 06:03
  • And in your browser developer tools under the network tab do you see any response? – David Edel Oct 22 '20 at 12:09

0 Answers0