I'm building an APP which need to be published in both the IOS & Play Store.
Tech Stack
.NET MAUI Blazor project, the entire app is rendered inside a BlazorWebView. All data is retrieved via an API.
Problem statement
I have a page which contains multiple PDF downloads. I want the app to open the default PDF app installed on that device. Is this possible?
My attempts
I've tried the classic Blazor WASM way by using a javascript function (this works without MAUI wrapper), but it's not working in the current setup. No error, the app just doesn't return a file. Any tips from the MAUI / Blazor community are very welcome. I want to avoid having to write native code.
I'm sharing my current approach (which works in WASM without the MAUI wrapper)
C#
public async Task<File> DownloadPdf(string url, string name)
{
using (HttpClient client = new HttpClient())
{
HttpResponseMessage response = await client.GetAsync(url);
if (response.IsSuccessStatusCode)
{
var file = await response.Content.ReadAsByteArrayAsync();
await JS.InvokeVoidAsync("BlazorDownloadFile", $"update.pdf", "application/pdf", file);
}
}
}
Javascript
window.BlazorDownloadFile = (filename, contentType, content) => {
// Create the URL
const file = new File([content], filename, { type: contentType });
const exportUrl = URL.createObjectURL(file);
// Create the <a> element and click on it
const a = document.createElement("a");
document.body.appendChild(a);
a.href = exportUrl;
a.download = filename;
a.target = "_self";
a.click();
// We don't need to keep the object URL, let's release the memory
// On older versions of Safari, it seems you need to comment this line...
URL.revokeObjectURL(exportUrl);
}