How to save/share a file in Net. Maui without savefiledialog?

1.3k Views Asked by At

My MauiBlazorApp can create a .pdf file. In what ways can I make this file accessible for the users of windows/android/IOS. A SaveFile Dialog is not yet implemented in Maui. With this path

string SaveToPath = System.IO.Path.Combine(FileSystem.Current.AppDataDirectory, "hello.pdf");

I can save it on a windows machine and open it (tested). The path (FileSystem.Current.AppDataDirectory) on a local android device (phone connected to windows maschine via usb) is something like this:

/data/user/0/com.companyname.appname/files/hello.pdf

But later I can't find this folder on my phone. I find this:

/data/com.companyname.appname/cache/ empty folders all the way

Why can't I save a file in .../downloads

How do you import/export data from 'device to device' in Maui? Files seems to be not the way. Email with attechments? Is that possible/better?

1

There are 1 best solutions below

4
Guangyu Bai - MSFT On

You can check the Maui File picker, It provides the method you can pick the file from the device.

public async Task<FileResult> PickAndShow(PickOptions options)
{
    try
    {
        var result = await FilePicker.Default.PickAsync(options);
        if (result != null)
        {
            if (result.FileName.EndsWith("jpg", StringComparison.OrdinalIgnoreCase) ||
                result.FileName.EndsWith("png", StringComparison.OrdinalIgnoreCase))
            {
                using var stream = await result.OpenReadAsync();
                var image = ImageSource.FromStream(() => stream);
            }
        }

        return result;
    }
    catch (Exception ex)
    {
        // The user canceled or something went wrong
    }

    return null;
}

In addition, you can refer to Folder Picker .NET MAUI. This is more detailed.