mysteryx93 / HanumanInstitute.MvvmDialogs

Library simplifying the concept of opening dialogs from a view model when using MVVM
MIT License
173 stars 9 forks source link

Proper use of dialogService.ShowOpenFileDialogAsync #25

Closed msh2050 closed 10 months ago

msh2050 commented 11 months ago

I'm using Avalonia 11.05 with .net7 the result.OpenReadAsync() is working with desktop and Android but not with the Web platform. how can I check if the file is loaded successfully? And in the case of larga file, how I can read the file uploading status.

private async Task OpenFileAsync()
{

    var settings = GetSettings(false);
    var result = await _dialogService.ShowOpenFileDialogAsync(this, settings);

    if (result?.Path != null)
    { 
        using var stream =  result.OpenReadAsync();

        ReadExcel(stream.Result);
    }

}
mysteryx93 commented 10 months ago

I've just fixed the Web demo so it at least runs... it's going to require a little bit of work

mysteryx93 commented 10 months ago

Browser app really isn't behaving well.

Posted some issues there https://github.com/AvaloniaUI/Avalonia/issues/14065

msh2050 commented 10 months ago

I managed to make it work with the browser and I added the upload status this is my code `maybe you need to remove some extra code that I add for my specific needs'

var ms = new MemoryStream();
var settings = GetSettings();
var result = await _dialogService.ShowOpenFileDialogAsync(this, settings);
IProgress<long> progress = new SynchronousProgress<long>(value =>
{
    ProgressBarCurrentValue = value;
    StatusText = value.ToString();
});

if (result?.Path != null)
{

    StatusText = result.Name;
    using var stream = result.OpenReadAsync();
    StatusText += " Open Read Done";
    //await stream.Result.CopyToAsync(ms);
    await stream.ContinueWith(async t =>
    {

        StatusText += " getting the result";
        var streamResult = t.Result;
        StatusText += " Result size: " + streamResult.Length + " starting copy to memory";
        await streamResult.CopyToAsync(ms, progress, source.Token, 1024);
        StatusText += "Done";
        ms.Position = 0;
        StatusText += " size:" + ms.Length;
        await ReadExcel(ms).ConfigureAwait(false);

    });

}
mysteryx93 commented 10 months ago

Oh perfect!

I'll add this to the samples.

mysteryx93 commented 10 months ago

btw where do you get your source.Token?

msh2050 commented 10 months ago

btw where do you get your source.Token?

static readonly CancellationTokenSource source = new ();

mysteryx93 commented 10 months ago

you just created your own to have a cancel button?

msh2050 commented 10 months ago

You just created your own to have a cancel button?

yes, but I have not implemented it yet. I just want to test this but I may not need it since I have no large files to upload