Skip to content Skip to sidebar Skip to footer

How To Download 2 Files In Xamarin Android

I want to download both .pdf and .mobi file in button click using Xamarin andorid. How can we trigger multiple file download using webclient?

Solution 1:

You can start downloading both files using HttpClient and Task.WhenAll in order to download at the same time.

        ...
        await Task.WhenAll(DownloadPDF(), DownloadMobi());
    }

    private async Task DownloadPDF()
    {
        var httpclient = new HttpClient(new AndroidClientHandler());
        using (var stream = await httpclient.GetStreamAsync("http://files/file.pdf"))
        using (var file = System.IO.File.Create("path/to/file.pdf"))
        {
            await stream.CopyToAsync(file);
            await file.FlushAsync();
        }
    }

    private async Task DownloadMobi()
    {
        var httpclient = new HttpClient(new AndroidClientHandler());

        using (var stream = await httpclient.GetStreamAsync("http://files/file.mobi"))
        using (var file = System.IO.File.Create("path/to/file.mobi"))
        {
            await stream.CopyToAsync(file);
            await file.FlushAsync();
        }            
    }

Post a Comment for "How To Download 2 Files In Xamarin Android"