6

I have some problem. I download a file after clicking an icon on web application. My next step is executed before the record file was downloaded. I want to wait until file is downloaded?

Does anyone know how to wait for that?

manjeet lama
  • 185
  • 3
  • 12
RafalQA
  • 127
  • 2
  • 3
  • 12

4 Answers4

6

I use the following script (filename should be passed in).

The first part waits till the file appears on disk (fine for chrome)

The second part waits till it stops changing (and starts to have some content)

var downloadsPath = Environment.GetEnvironmentVariable("USERPROFILE") + @"\Downloads\" + fileName;
for (var i = 0; i < 30; i++)
{
    if (File.Exists(downloadsPath)) { break; }
    Thread.Sleep(1000);
}
var length = new FileInfo(downloadsPath).Length;
for (var i = 0; i < 30; i++)
{
    Thread.Sleep(1000);
    var newLength = new FileInfo(downloadsPath).Length;
    if (newLength == length && length != 0) { break; }
    length = newLength;
}
Dmitry
  • 86
  • 5
1

I started from Dmitry`s answer and added some support for controlling the timeouts and the polling intervals. This is the solution:

/// <exception cref="TaskCanceledException" />
internal async Task WaitForFileToFinishChangingContentAsync(string filePath, int pollingIntervalMs, CancellationToken cancellationToken)
{
    await WaitForFileToExistAsync(filePath, pollingIntervalMs, cancellationToken);

    var fileSize = new FileInfo(filePath).Length;

    while (true)
    {
        if (cancellationToken.IsCancellationRequested)
        {
            throw new TaskCanceledException();
        }

        await Task.Delay(pollingIntervalMs, cancellationToken);

        var newFileSize = new FileInfo(filePath).Length;

        if (newFileSize == fileSize)
        {
            break;
        }

        fileSize = newFileSize;
    }
}

/// <exception cref="TaskCanceledException" />
internal async Task WaitForFileToExistAsync(string filePath, int pollingIntervalMs, CancellationToken cancellationToken)
{
    while (true)
    {
        if (cancellationToken.IsCancellationRequested)
        {
            throw new TaskCanceledException();
        }

        if (File.Exists(filePath))
        {
            break;
        }

        await Task.Delay(pollingIntervalMs, cancellationToken);
    }
}

And you use it like this:

using var cancellationTokenSource = new CancellationTokenSource(timeoutMs);
WaitForFileToFinishChangingContentAsync(filePath, pollingIntervalMs, cancellationToken);

A cancellation operation will be automatically triggered after the specified timeout.

Zeronoctis
  • 13
  • 5
1

You can check if there is a temporary file in the download directory

I use to listen to folder from time to time

The example below is for Google Chrome driver

try
{
    while (Directory.GetFiles(downloadPath2).Count(i => i.EndsWith(".crdownload")) > 0)
    {
        Thread.Sleep(2000);
    }
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
    Console.WriteLine(ex.StackTrace);
}
Diego Montania
  • 322
  • 5
  • 12