Select theme:

Copy and download file when deploying application (Blazor)Link to this section

This guide demonstrates how to copy and download file when deploying application.

Step by stepLink to this section

1. Create new Blazor application. image

2. Add desired file(s) in server folder [APP_NAME].csproj file

<ItemGroup>
    <None Include="ProductSales.pdf" CopyToPublishDirectory="Always" />
    ...

image

3. Execute custom method to download the file.

image

MainPage.razor.cs

using System.Threading.Tasks;

namespace SampleBlazor.Pages
{
    public partial class MainPageComponent
    {
        public async Task GetFile(string fileName)
        {
            UriHelper.NavigateTo($"api/custommethod/getfile?fileName={fileName}", true);
        }
    }
}

image

CustomController.cs

using System.IO;
using Microsoft.AspNetCore.Mvc;

namespace SampleBlazor
{
    [Route("api/[controller]/[action]")]
    public class CustomMethodController : Controller
    {
        [HttpGet]
        public IActionResult GetFile(string fileName)
        {
            if (System.IO.File.Exists(fileName))
            {
                return File(System.IO.File.ReadAllBytes(fileName), contentType: "application/pdf", fileName);
            }
            return NotFound();
        }
    }
}

4. Run the application and download file. image

In this article -
© 2016-2026 Radzen Ltd. All Rights Reserved.
Designed and developed with ❤️ in Radzen Blazor Studio.
IP Geolocation by DB-IP

Select theme: