Search code examples
c#androidmauimega-api

Downloading files with Mega API in .NET MAUI on Android


I'm trying to download an image from a remote public Mega folder to an Android device using the Mega API in .NET MAUI.

This is the code:

namespace Junior_Jobs;

using Google.Apis.Auth.OAuth2;
using Google.Apis.Drive.v3;
using Google.Apis.Services;
using CG.Web.MegaApiClient;
using Microsoft.Maui.Controls.PlatformConfiguration;
using Android.OS;
using System;

public partial class Tryimportimage : ContentPage
{
    public Tryimportimage()
    {
        InitializeComponent();
        MegaApiClient client = new MegaApiClient();
        client.LoginAnonymous();

        Uri fileLink = new Uri("https://mega.nz/file/W0UAgJaK#XOYyTETrIy8daz3_dw3fdh6Hh8EFEdrnbyoop1r9R6g");
        INode node = client.GetNodeFromLink(fileLink);

        System.Diagnostics.Debug.WriteLine($"Downloading {node.Name}");
        client.DownloadFileAsync(fileLink, Path.Combine("/storage/emulated/0/Downloads",node.Name));
        
        client.Logout();
    }
}

I don't get any errors at the moment, but the code doesn't do anything either.


Solution

  • First of all, you should neither execute asynchronous code nor do any heavy lifting inside of constructors.

    Secondly, you should await the DownloadFileAsync() method:

    await client.DownloadFileAsync(fileLink, Path.Combine("/storage/emulated/0/Downloads",node.Name));
    

    Since this cannot be done inside the constructor, because constructors in C# cannot be asynchronous, you'll have to move the code into an async method instead:

    namespace Junior_Jobs;
    
    using Google.Apis.Auth.OAuth2;
    using Google.Apis.Drive.v3;
    using Google.Apis.Services;
    using CG.Web.MegaApiClient;
    using Microsoft.Maui.Controls.PlatformConfiguration;
    using Android.OS;
    using System;
    using System.Threading.Tasks;
    
    public partial class Tryimportimage : ContentPage
    {
        public Tryimportimage()
        {
            InitializeComponent();
        }
    
        public async Task DownloadAsync()
        {
            MegaApiClient client = new MegaApiClient();
            client.LoginAnonymous();
    
            Uri fileLink = new Uri("https://mega.nz/file/W0UAgJaK#XOYyTETrIy8daz3_dw3fdh6Hh8EFEdrnbyoop1r9R6g");
            INode node = client.GetNodeFromLink(fileLink);
    
            System.Diagnostics.Debug.WriteLine($"Downloading {node.Name}");
            await client.DownloadFileAsync(fileLink, Path.Combine("/storage/emulated/0/Downloads",node.Name));
            
            client.Logout();
        }
    }
    

    Then you can instantiate the Tryimportimage page and await the result, e.g. like this, depending on how you create your page:

    public async Task OpenImportPageAsync()
    {
        var page = new Tryimportimage();
        await Navigation.PushAsync(page);
        await page.DownloadAsync();
    }
    

    Finally, downloading files and such should usually be delegated to service classes or ViewModels. It's not a common approach to do this directly in a View's code-behind.

    You may want to read about asynchronous programming using the async-await pattern.