How to use WebReport in the Knockout.js application
The Knockout.js library is used to create web applications. Due to the support of this library in Microsoft Visual Studio, we can use TypeScript and backend based on ASP .Net Core MVC. The last means that we will be able to use FastReport.Net reports. It remains only to display the report in the client application. That is exactly we will do in this article.
To use knockout with .Net Core, you must have .Net Core SDK 2.0 or MS Visual Studio installed. By default, the application template with the knockout library is not available to you. Therefore, you need to install it with just one command. At the Windows command promt, enter:
dotnet new — install Microsoft.AspNetCore.SpaTemplates::*
After that, you can create a spa application based on knockout. In the desired folder, open the command line and enter the command:
dotnet new knockout –o KnockWebReport
After creating the application, go to the folder with the created application and restore the necessary packages using the command:
Now you can open the created project. Our goal is to create a Web report of FastReport. Therefore, we install the FastReport packages. To do this, open the package manager and configure the local package source to the Nuget folder in the FastReport.Net installation directory. After that, we have a set of FastReport packages available for installation. Install FastReport.Core and FastReport.Web.
Add the App_Data folder to the wwwroot directory. In it we place the database file for reports:
Let's use the existing SampleDataController controller. Remove all the methods from it and add our own:
The ShowReport method loads the specified report template into the WebReport object and displays it. And the Upload method gets the file from the client, saves it to the server and returns the name of the saved file.
For the ShowReport method we create a view:
With the following code:
We now turn to the client application. It is located in the ClientApp folder:
Let’s use the home page to display the report. Edit the code in the file home-page.html:
We display the button that opens the standard open file window. And also, depending on the value of the logical parameter ‘show’, we output the frame with the web object of the report.
Now we will write a script for this template in the home-page.ts file:
In this script, we implemented the function of uploading a file to the server. POST request is executed, as a result of which we receive the name of the saved file from the server. Next, assign the url variable to the path of the report display method, taking into account the report name received. As a result, we get a web report.
Let's run our application and see what we have.
Select the report file in frx format.
And we get a report on your web page.
Thus, we were convinced that displaying the FastReport.Net report in a knockout-based web application is easy.