Download file - blazor


what would be the best approach when downloading file from a database (uploaded with fileInput)? My idea is to put a button that would initialize the download, but I'm not sure what would be the best approach either to download a file or pass it to browser to display it (pdf). Can this be done through invoking a custom method? I'd like to use radzen ide and blazor components as much as possible. Some examples on the net are involving some JSinterop but I would rather avoid it. I'm sure someone already has done it, I would really appreciate directions.

kind regards, martin

Check this thread for reference:

There are also other threads in our forum discussing exactly the same - you can use search to find them.

Thnx. I have gone through all relevant posts. I have implemented a custom class that returns FileStreamResult. I have binded the call (getfile(id)) on the click event of the button, but this does not start the download on client side, though I can see that the event is fired and data is returned. I must be missing something?

kind regards, martin

Maybe you are missing this:

Maybe for all those looking for a solution.
If you used a FileInput file and looking for the other half of the code to download or show document here is one of the shortest paths.
The simplest way is to create another controller

public IActionResult GetDocument(int id)

        var queryresult = Context.Documents.Where(doc => == id).FirstOrDefault();
        //var queryresult = Context.Documents.Where(query).FirstOrDefault();

        if (queryresult == null || queryresult.document1 == null)            
            return NotFound();
        var storedstring = queryresult.document1;       //the Base URI encoded string (if uploaded through fileInput --> starts with "data: mime ;base64,")
        var filename = queryresult.filename;
        if (!filename.EndsWith(".pdf")) filename += ".pdf";
        var mime = storedstring.Substring(storedstring.IndexOf(':') + 1, storedstring.IndexOf(';') - storedstring.IndexOf(':') - 1);
        storedstring = storedstring.Replace("data:" + mime + ";base64,", "");
        byte[] byteArr = Convert.FromBase64String(storedstring);

        // Response...
        System.Net.Mime.ContentDisposition cd = new System.Net.Mime.ContentDisposition
            FileName = filename,
            Inline = false  // false = prompt the user for downloading;  true = browser to try to show the file inline
        Response.Headers.Add("Content-Disposition", cd.ToString());
        Response.Headers.Add("X-Content-Type-Options", "nosniff");

        return File(byteArr, mime);

since the response has to come from the server with a specific headers so browser will recognize it. Than all that is left is to create a Button and bind its Click event to a c# code that navigates to download controller:

navigationManager.NavigateTo("download/GetDocument?id="+${}, true);

To create a preview window (for pdf) I have created a new Button and bind click event to a modal window (showdocument) and I would pass a document id to it. The page would be a single iframe:

<iframe height="600" width="500" src="download/show/ShowDocument?id=${id}" border="0" ></iframe>

And as you can see I have created another method showdocument in the download controller that hosts the same code except that the content disposition would have Inline attribute to true.

Basically this is your simple upload/download/show documents feature.

kind regards, martin