How to download an in-memory object as a JSON file to the client from server side Blazor?

Anthony 21 Reputation points
2021-10-12T22:41:31.367+00:00

I have a class containing some info a user might want to download after visiting a page.

MyClass.cs

public class MyClass  
    {  
        public int Count { get; set; }  
        public string Summary { get; set; }  
    }  

This answer seems related to what I want to do: https://learn.microsoft.com/en-us/answers/questions/243420/blazor-server-app-downlaod-files-from-server.html but using in-memory objects instead of files on the server location.

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,208 questions
Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,403 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,307 questions
{count} votes

Accepted answer
  1. Zhi Lv - MSFT 32,021 Reputation points Microsoft Vendor
    2021-10-13T08:34:24.633+00:00

    Hi @Anthony ,

    For the in-memory objects, you can use System.Text.Json.JsonSerializer.Serialize() method to convert the objects to the json string, then convert the string to byte array using the System.Text.ASCIIEncoding.ASCII.GetBytes() method, after that you can also use the File() method to export the data to a json file.

    Code as below:

    Download Razor page:

    Download.cshtml:

    @page "/download"  
    @model BlazorServerSample.Pages.DownloadModel  
    @{  
    }  
    

    Download.cshtml.cs:

    public class DownloadModel : PageModel  
    {  
        private readonly IWebHostEnvironment _env;  
        private readonly WeatherForecastService _forecastService;  
    
        public DownloadModel(IWebHostEnvironment env, WeatherForecastService weatherForecastService)  
        {  
            _env = env;  
            _forecastService = weatherForecastService;  
        }  
        public async Task<IActionResult> OnGetAsync()  
        {   
            //get forecast from forecast service  
            var forecasts = await _forecastService.GetForecastAsync(DateTime.Now);  
            var jsonstr = System.Text.Json.JsonSerializer.Serialize(forecasts);  
            byte[] byteArray = System.Text.ASCIIEncoding.ASCII.GetBytes(jsonstr);  
                
            return File(byteArray, "application/force-download", "file1.json");  
        }  
    }  
    

    The WeatherForecast class:

    public class WeatherForecast  
    {  
        public DateTime Date { get; set; }   
        public int TemperatureC { get; set; }   
        public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);   
        public string Summary { get; set; }  
    }  
    

    In the main page, add the following download button:

    <a class="form-control btn btn-primary" href="/download">Download</a>  
    

    The result as below:

    140115-7.gif


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Dillion


0 additional answers

Sort by: Most helpful