Select theme:

Material 3

Copy and download file when deploying application (Angular)link

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

Source Code

Step by steplink

1. Create new application with .NET server-side project. image

2. Add desired file(s) in project.csproj\project.vbproj

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

image

3. Execute custom method similar to this article to download the file.

image

import { Component, Injector } from '@angular/core';
import { MainPageGenerated } from './main-page-generated.component';

// Import HttpClient and HttpParams which are needed to make HTTP calls.
import { HttpClient, HttpParams } from '@angular/common/http';
// Import the environment to get the server URL
import { environment } from '../../environments/environment';

@Component({
  selector: 'main-page',
  templateUrl: './main-page.component.html'
})
export class MainPageComponent extends MainPageGenerated {
  constructor(injector: Injector, private http: HttpClient) {
    super(injector);
  }

  getFile(fileName: string) {
    // Extract the server URL
    const url = environment.sample.replace(/odata.*/, '');

    return this.http
      // Request the GetFile action method
      .get(`${url}api/custommethod/getfile`, {
        responseType: 'blob',
        params: new HttpParams().set('fileName', fileName)
      })
      .map((value: Blob, index: number) => {
        // Force browser to show downloaded content as file
        if (navigator.msSaveBlob) {
          navigator.msSaveBlob(value, fileName);
        } else {
          const link = document.createElement("a");
          if (link.download !== undefined) {
            link.setAttribute("href", URL.createObjectURL(value));
            link.setAttribute("download", fileName);
            link.style.visibility = 'hidden';
            document.body.appendChild(link);
            link.click();
            document.body.removeChild(link);
          }
        }
      }).toPromise();
  }
}

image

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

namespace Sample.Controllers
{
    [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");
            }
            return NotFound();
        }
    }
}
Imports System.IO
Imports Microsoft.AspNetCore.Mvc

Namespace Sample.Controllers
    <Route("api/[controller]/[action]")>
    Public Class CustomMethodController
        Inherits Controller

        <HttpGet>
        Public Function GetFile(ByVal fileName As String) As IActionResult
            If System.IO.File.Exists(fileName) Then
                Return File(System.IO.File.ReadAllBytes(fileName), contentType:="application/pdf")
            End If

            Return NotFound()
        End Function
    End Class
End Namespace

image

4. Deploy the application to IIS and download file. image image image

In this article —
© 2016-2025 Radzen Ltd. All Rights Reserved.
Designed and developed with ❤️ in Radzen Blazor Studio.

Select theme:

Material 3