question

Anthony-1098 avatar image
0 Votes"
Anthony-1098 asked ZhiLv-MSFT commented

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

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://docs.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.

dotnet-csharpdotnet-aspnet-core-blazordotnet-aspnet-core-razor
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

The following is done in a unit test using this data https://jsonplaceholder.typicode.com/users/1, see if this might work.


1 Vote 1 ·

1 Answer

ZhiLv-MSFT avatar image
0 Votes"
ZhiLv-MSFT answered ZhiLv-MSFT commented

Hi @Anthony-1098,

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


7.gif (306.4 KiB)
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

This works, thank you!

0 Votes 0 ·

Follow up comment, how can I pass my object over to this download model/page?

0 Votes 0 ·

Hi @Anthony-1098,

 var forecasts = await _forecastService.GetForecastAsync(DateTime.Now);

At the above sample, in the Blazor component and the Download Razor page, we can get the data via the _forecastService service. After clicking the download button, it will re-call the _forecastService services to get the data then export them to the json file. You could also change the _forecastService service as the database. Then, if there have some filter or search parameter, you can transfer them via the request url (append the parameter at the end the url), then in the Download razor page (get the parameter via url), re-query the database based on the parameters, after that export the data to json file.

0 Votes 0 ·