Hi ,
I have a huge amount of files ( Word, Excel , MSG ) on the webserver, these files are not in the database.
How can I create a download link from within Radzen (Angular).
I wanted to ask before using any additional third party or custom modules .
Case:
Documents stored on filesystem on server : \servername\share\folder\file.docx
In database I have a table with the name of the file and server location to the folder and file .
I want to make this file downloadable for the users of the application.
Searched the forum but can not find , probably using the wrong keywords.
Thank you for your help.
Radzen does not support this out of the box. You can however use a custom server method for that (or create another controller). Here is a sample implementation of such a method (inspired by this SO question):
[Route("api/[controller]/[action]")]
public class ServerMethodsController
{
// Inject a EF context to your database
// Check https://www.radzen.com/documentation/invoke-custom-method/#create-the-custom-method-that-executes-the-db-query
MyDataSourceContext context;
public ServerMethodsController(MyDataSourceContext context)
{
this.context = context;
}
[HttpGet("download")]
public IActionResult GetBlobDownload([FromQuery] string fileName)
{
// Find the local file name from the DB
var file = context.Files.Where(file => file.FileName == fileName).FirstOrDefault();
if (file == null || !File.Exists(file.FileName))
{
return NotFound();
}
// Get a stream to the file
using var content = System.IO.File.OpenRead(file.FileName);
// Return the file
return File(content, "application/octet-stream", fileName);
}
}
Then to use it in your Angular pages create links to '/api/servermethods/download?fileName=<required filename>' for example '/api/servermethods/download?fileName=test.xlsx'
If you continue to get Angular router errors then ASP.NET still cannot match the URL of the action because it is wrong. As a result it returns the index.html page in order to properly support the Angular router.
I am afraid I don't understand what the problem is. This is quite a simple thing and is basically the way Radzen implements custom server methods. I still think you are not using the right URL since it never hits the action even when requested from the browser address bar. If you need further assistance you can send a runnable version of your project to info@radzen.com
the reset password , default route /auth/ does also not work , so there is something else going on and not just with the download.
I wil check with my other applications if this is not due to an update or change .
Hi Krochev,
Is it possible for you to create an working example of this sample? I have tried all sorts of routing changes and this action does not hit.
hit the wall here..
invoking it from a button works.
I was using your suggestion to do it from a link.
I have a datagrid and have used a template for one column
Added a link component and given the path : /api/ServerMethods/filedownload/${data.document_id}
In the datagrid it creates a nice link to click on:
but does not route .
The custom method returns a File to the button click result, wil this invoke an file download in the browser ?
Relatives url will not work in the development since the file will be served from the .NET application running on port 5000 while the Angular application is served on port 8000 and the Angular rooter will try to open the url first. Here is an example with real file downloaded from a link where fileName is a page property initialized on page load: