Check If Download Is Completed

python selenium, find out when a download has completed?

There is no built-in to selenium way to wait for the download to be completed.


The general idea here would be to wait until a file would appear in your "Downloads" directory.

This might either be achieved by looping over and over again checking for file existence:

  • Check and wait until a file exists to read it

Or, by using things like watchdog to monitor a directory:

  • How to watch a directory for changes?
  • Monitoring contents of files/directories?

How to check if file download is complete

Worked when I made these changes to the code: Changed input string to URI, fixed local path, used the correct eventhandler and did Console.Read in the end. I have shortened the code a bit, but you get the idea:

static void Main(string[] args)
{
using (WebClient myWebClient = new WebClient())
{
myWebClient.DownloadFileCompleted += DownloadCompleted;
myWebClient.DownloadFileAsync(new Uri("http://someUrl"), @"e:\file.mp3");
}

Console.ReadLine();
}

public static void DownloadCompleted(object sender, AsyncCompletedEventArgs e)
{
Console.WriteLine("Success");
}

check if download is completed

That's not really going to tell you if the download has completed for the user. It will tell you when you have finished sending bytes to the user, but it says nothing about how many of them the user has actually received.

In reality, there is no way with PHP (which is a server-side, not a client-side, language) to truly detect when a file download has completed. The best you can do is log the download in your database when it begins. If you absolutely, completely and totally need to know when the download has completed, you'll have to do something like embed a Java applet or use Flash. However, usually that is not the correct answer in terms of usability for your user (why require them to have Java or Flash installed just to download something from you?).

How to check if file downloads finished in Chrome?

The blur event is just a fragile workaround that might work in a browser or not.

The blur event means, that something is losing focus. It might be that Firefox browser (among others) does indeed blur the link you clicked on, but anything else might blur that link, too (Changing the tab, Clicking on the loading Screen, etc). Which is what you might NOT want.

And because HTTP is a state less protocol, we cannot know on the client side, when this is finished without the help from the server. And you cannot rely on JS to detect this, because the download of the file is 'in a different tab', in a different context which you do not have access to.

The only solution I can imagine (and other's, too, as it seems): When the download starts, start a JS interval that regularly (like every 500ms or so) checks the server for a download status. On the server side you must identify the download to the user when it starts and respond the status back to the client when asked. How this works depends, of course, on the server environment and programming language you use. I know, you can make it work in PHP, but I don't know of all the other languages.

BTW: I would call the 'loading screen' a 'loading indicator', because a loading screen (or splash screen) is a picture that is shown once before a software loads (at least on my computer/brain).

How do I check for a file to be finished downloading Python3?

When urlretrieve returns, the file has already finished downloading.

See the usage example from the docs:

>>> import urllib.request
>>> local_filename, headers = urllib.request.urlretrieve('http://python.org/')
>>> html = open(local_filename)

As can be seen, the file is opened immediately after the call to urlretrieve, as it was already created and the content was already written there.

Detect when a browser receives a file download

One possible solution uses JavaScript on the client.

The client algorithm:

  1. Generate a random unique token.
  2. Submit the download request, and include the token in a GET/POST field.
  3. Show the "waiting" indicator.
  4. Start a timer, and every second or so, look for a cookie named "fileDownloadToken" (or whatever you decide).
  5. If the cookie exists, and its value matches the token, hide the "waiting" indicator.

The server algorithm:

  1. Look for the GET/POST field in the request.
  2. If it has a non-empty value, drop a cookie (e.g. "fileDownloadToken"), and set its value to the token's value.

Client source code (JavaScript):

function getCookie( name ) {
var parts = document.cookie.split(name + "=");
if (parts.length == 2) return parts.pop().split(";").shift();
}

function expireCookie( cName ) {
document.cookie =
encodeURIComponent(cName) + "=deleted; expires=" + new Date( 0 ).toUTCString();
}

function setCursor( docStyle, buttonStyle ) {
document.getElementById( "doc" ).style.cursor = docStyle;
document.getElementById( "button-id" ).style.cursor = buttonStyle;
}

function setFormToken() {
var downloadToken = new Date().getTime();
document.getElementById( "downloadToken" ).value = downloadToken;
return downloadToken;
}

var downloadTimer;
var attempts = 30;

// Prevents double-submits by waiting for a cookie from the server.
function blockResubmit() {
var downloadToken = setFormToken();
setCursor( "wait", "wait" );

downloadTimer = window.setInterval( function() {
var token = getCookie( "downloadToken" );

if( (token == downloadToken) || (attempts == 0) ) {
unblockSubmit();
}

attempts--;
}, 1000 );
}

function unblockSubmit() {
setCursor( "auto", "pointer" );
window.clearInterval( downloadTimer );
expireCookie( "downloadToken" );
attempts = 30;
}

Example server code (PHP):

$TOKEN = "downloadToken";

// Sets a cookie so that when the download begins the browser can
// unblock the submit button (thus helping to prevent multiple clicks).
// The false parameter allows the cookie to be exposed to JavaScript.
$this->setCookieToken( $TOKEN, $_GET[ $TOKEN ], false );

$result = $this->sendFile();

Where:

public function setCookieToken(
$cookieName, $cookieValue, $httpOnly = true, $secure = false ) {

// See: http://stackoverflow.com/a/1459794/59087
// See: http://shiflett.org/blog/2006/mar/server-name-versus-http-host
// See: http://stackoverflow.com/a/3290474/59087
setcookie(
$cookieName,
$cookieValue,
2147483647, // expires January 1, 2038
"/", // your path
$_SERVER["HTTP_HOST"], // your domain
$secure, // Use true over HTTPS
$httpOnly // Set true for $AUTH_COOKIE_NAME
);
}


Related Topics



Leave a reply



Submit