Pause and resume loading in flex?

Can I start a download in an air application, pause it, and then resume it?

I want to download very large files (1-3Gb), and I have to be sure that the connection will be interrupted, and the next time the user tries to download the file, he will start from the last position.

Any ideas and source code examples would be appreciated.

+3
source share
2 answers

Yes, you would like to use the URLStream class (URLLoader does not support partial downloads), and the HTTP range header . Note that there are some onerous security restrictions in the Range header, but this should be fine in an AIR application. Here's some unverified code that should give you a general idea.

private var _us:URLStream;
private var _buf:ByteArray;
private var _offs:uint;
private var _paused:Boolean;
private var _intervalId:uint;
...
private function init():void {
    _buf = new ByteArray();
    _offs = 0;

    var ur:URLRequest = new URLRequest( ... uri ... );
    _us = new URLStream();

    _paused = false;
    _intervalId = setInterval(500, partialLoad);
}
...
private function partialLoad():void {
    var len:uint = _us.bytesAvailable;
    _us.readBytes(_buf, _offs, len);
    _offs += len;

    if (_paused) {
        _us.close();
        clearInterval(_intervalId);
    }
}
...
private function pause():void {
    _paused = true;
}
...
private function resume():void {
    var ur:URLRequest = new URLRequest(... uri ...);
    ur.requestHeaders = [new URLRequestHeader("Range", "bytes=" + _offs + "-")];
    _us.load(ur);
    _paused = false;
    _intervalId = setInterval(500, partialLoad);
}
+5
source

If you’re targeting mobile devices, you might want to take a look at this native extension: http://myappsnippet.com/download-manager-air-native-extension/ supports simultaneous renewable downloads with multiple-section fragments for downloading files as possible faster.

0
source

Source: https://habr.com/ru/post/1728090/


All Articles