expressappframework-404760-document-management-file-attachments-module-how-to-download-a-file-in-xaf-blazor.md
This topic describes different ways to enable file download functionality in XAF ASP.NET Core Blazor applications. Examples in this topic use the tutorial application for demonstration purposes, but you can easily adapt these steps to suit your needs.
For information on how to upload files in XAF Blazor, refer to the following topic: How to: Use a Custom Action to Upload a File in a Popup Window.
XAF does not implement file download mechanisms and relies on ASP.NET Core API. The following sections contain examples that use this API in XAF Blazor applications:
Microsoft documentation describes two mechanisms to download files:
Download a file from a stream (recommended for files smaller than 250 MB).
Download a file from a URL (recommended for files larger than 250 MB).
This topic demonstrates both of these approaches.
Note
The sample below uses a void-returning async method for the Execute event handler. If you need to modify the suggested code, be aware that the caller of a void-returning async method cannot catch exceptions thrown by the method. Such unhandled exceptions may cause application failure. For more information about void-returning async methods, refer to the following Microsoft article: Async return types (C#) - Void return type.
The example below uses JavaScript code to download a file. This code takes a link to a .NET stream (DotNetStreamReference) as a parameter. The application then calls this JavaScript code using the IJSRuntime service.
To integrate this file download method into your application, follow the steps below:
Add the following JavaScript code to the <body> tag of your Blazor project’s YourSolutionName.Blazor.Server/Pages/_Host.cshtml file:
Add a new file (DownloadFileController.cs) to your Blazor project. This file should contain a View Controller that allows files to be downloaded.
Run the application and click Download a File. The specified file is downloaded to the browser.
To integrate this file download method into your application, follow the steps below:
Add the following JavaScript code to the <body> tag of your Blazor project’s YourSolutionName.Blazor.Server/Pages/_Host.cshtml file:
Create an endpoint (MVC controller) that provides access to the desired file. To do this, add a new file (FileController.cs) to the API folder of your Blazor project. Note that this controller should contain the Download method.
If your project includes an authorization setting (AddAuthorization or AddAuthorizationBuilder), use the CookieAuthenticationDefaults.AuthenticationScheme setting to allow cookies in the project. The back-end can then use cookies to authorize download requests initiated by the Blazor application. For more endpoint-related notes, refer to the corresponding section below.
Add a new file (DownloadFileController.cs) with a View Controller to your Blazor project.
Run the application and click Download a File. The specified file is downloaded to the browser.
{navigationManager.BaseUri}IFileUrlService/?objectType= MySolution.Module.BusinessObjects.Resume&objectKey=1dc9dfd3-7389-4e17-930c-c4018deb4acc&propertyName=File URL pattern instead of implementing your own API controller.IFileData objects. This service has similar functionality to IFileUrlService mentioned before but offers more customization options. We recommend that you use this endpoint if you use Web API in your project. In this case, use the following URL pattern: {navigationManager.BaseUri}api/MediaFile/DownloadStream?objectType=Employee&objectKey=0eb531f9-2195-49e4-60b1-08db8ba8ed44&propertyName=Photo.