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.

Developer technologies ASP.NET ASP.NET Core
Developer technologies .NET Blazor
Developer technologies C#
{count} votes

Accepted answer
  1. Anonymous
    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

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.