Reputation: 590
I have a project that is a Blazor Hybrid using Maui .NET. There exists the method FilePicker which allows you to open a file from the computer. However I cannot find a method to SAVE a file to the computer? May anyone know if this exists or how I can accomplish this if it doesn't?
Upvotes: 1
Views: 1218
Reputation: 10078
You can create your own SaveFile service
with the SaveFile Implemtation
.Here's the code snippet below for the WinUI example that you can refer to:
public sealed class SaveFile : ISaveFile
{
public async ValueTask SaveFileAsync(string filename, Stream stream)
{
var extension = Path.GetExtension(filename);
var fileSavePicker = new FileSavePicker();
fileSavePicker.SuggestedFileName = filename;
fileSavePicker.FileTypeChoices.Add(extension, new List<string> { extension });
if (MauiWinUIApplication.Current.Application.Windows[0].Handler.PlatformView is MauiWinUIWindow window)
{
WinRT.Interop.InitializeWithWindow.Initialize(fileSavePicker, window.WindowHandle);
}
var result = await fileSavePicker.PickSaveFileAsync();
if (result != null)
{
using (var fileStream = await result.OpenStreamForWriteAsync())
{
fileStream.SetLength(0); // override
await stream.CopyToAsync(fileStream);
}
}
}
}
Upvotes: 2