0

I have a controller which has a function which returns a .msg file in the format:

return File(emailOutput.ToArray(), "application/vnd.ms-outlook", "blahblah.msg");

My current front end @code block includes:

var response = await Http.GetAsync($"routewhichgetsthe return");

My question is what do I need to change on the front-end to allow for this returned file to be downloaded without storing it on the server? I'm not sure if the controller call needs changed, or how to get the HTML to allow a button press to activate the download.

JHBonarius
  • 10,824
  • 3
  • 22
  • 41

2 Answers2

0

If you Google this, you'll find pages like this one, which gives the following code example:

public async Task<string> DownloadFile(string guid)
{
    // validation
    _logger.LogInformation($"Downloading File with GUID=[{guid}].");
    var fileInfo = new FileInfo($"{guid}.txt");

    var response = await _httpClient.GetAsync($"{_url}/api/files?guid={guid}");
    response.EnsureSuccessStatusCode();
    await using var ms = await response.Content.ReadAsStreamAsync();
    await using var fs = File.Create(fileInfo.FullName);
    ms.Seek(0, SeekOrigin.Begin);
    ms.CopyTo(fs);

    _logger.LogInformation($"File saved as [{fileInfo.Name}].");
    return fileInfo.FullName;
}

Note, there's also a HttpClient.GetStreamAsync method.

edit: there's also an extensive answer on SO.

JHBonarius
  • 10,824
  • 3
  • 22
  • 41
0

Thanks for the help, there's actually an incredibly simple solution I was missing.

<a href="routewhichgetsthereturn" download>