2

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);
    }
0

1 Answer 1

2

I have solved it with the information found here: https://learn.microsoft.com/en-us/dotnet/maui/platform-integration/appmodel/launcher?tabs=android

If anyone needs it, this is my adjusted code which opens the PDF in the correct app!

        if (response.IsSuccessStatusCode)
        {
            IsDownloading = true;

            var pdfByteArray = await response.Content.ReadAsByteArrayAsync();

            // cache the file
            string file = System.IO.Path.Combine(FileSystem.CacheDirectory, $"{name.Replace(" ", "-")}.pdf");
            await System.IO.File.WriteAllBytesAsync(file, pdfByteArray);

            IsDownloading = false;
            await Launcher.Default.OpenAsync(new OpenFileRequest(name, new ReadOnlyFile(file)));
        }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.