HttpDownload

Description

The HttpDownload object provides a high-level interface for downloading files from HTTP servers. Once started via start() or the running property the download is performed asynchronously in the background. The progress can be monitored through the bytesReceived property. When finished the finished() signal is emitted and the running property changes to false.

› Inherits:Object

Properties

bytesReceived

This property holds the number of bytes already downloaded.

› Type:SignedBigInteger
› Signal:bytesReceivedChanged()
› Attributes:Readonly

bytesTotal

This property holds the total number of bytes to download.

› Type:SignedBigInteger
› Signal:bytesTotalChanged()
› Attributes:Readonly

error

This property holds the most recently occurred error or HttpDownload.NoError if no error occurred. If the same error occurs multiple times this property does not change. Use the errorOccurred() signal to detect multiple occurrences of the same error.

› Type:Error
› Signal:errorChanged()
› Attributes:Readonly

errorString

This property holds the current human readable error string corresponding to the current value in the error property. It may include additional information such as failure reasons or locations.

› Type:String
› Signal:errorStringChanged()
› Attributes:Readonly

outputFile

This property holds a File object representing the file which the downloaded data is written to.

› Type:File
› Signal:outputFileChanged()
› Attributes:Writable

request

This property holds the request used for initiating the download. It can be used for customizing e.g. the used HTTP headers.

› Type:HttpRequest
› Signal:requestChanged()
› Attributes:Writable

running

This property holds whether the download is currently running. Changing this property is equivalent to calling start() and stop(). After a download has been finished this property changes to false automatically.

› Type:Boolean
› Default:true
› Signal:runningChanged()
› Attributes:Writable

url

This property holds the URL of the file to download. It wraps the HttpRequest.url property and is provided for convenience only.

› Type:String
› Signal:urlChanged()
› Attributes:Writable

Methods

start()

This method starts the download if it’s not running already. It returns true if the download could be started succesfully. Otherwise an error is indicated through the error property. This method does not block. Instead the finished() signal is emitted when the download has been finished.

› Returns:Boolean

stop()

This method stops the download, i.e. aborts a running download. If it is not running, this method has no effect and does not raise an error.

Signals

errorOccurred()

This signal is emitted whenever an error has occurred, regardless of whether the error property has changed or not. In contrast to the change notification signal of the error property this signal is also emitted several times if a certain error occurs several times in succession.

finished()

This signal is emitted when a download has been finished. It’s also emitted if an error occurred while downloading.

Enumerations

Error

This enumeration describes all errors which can occur in HttpDownload objects. The most recently occurred error is stored in the error property.

Name Value Description
HttpDownload.NoError 0 No error occurred or was detected.
HttpDownload.AlreadyRunning 1 The download is already running and can’t be started.
HttpDownload.InvalidRequest 2 The request property is empty or invalid.
HttpDownload.InvalidOutputFile 3 The output file property is empty or invalid.
HttpDownload.OutputFileNotWritable 4 The output file can’t be opened for writing.

Example

import InCore.Foundation 2.5
import InCore.Http 2.5

Application {

    HttpDownload {
        url: "http://archive.ubuntu.com/ubuntu/dists/bionic/main/installer-amd64/current/images/netboot/mini.iso"
        outputFile: File { fileName: "mini.iso"; storage: InMemoryStorage { } }
        onBytesReceivedChanged:
            console.log("Download progress:",
                        (bytesTotal > 0 ? Math.round(bytesReceived * 100 / bytesTotal) : 0) + "%");
        onFinished: console.log(bytesTotal, "bytes have been downloaded successfully to",
                                outputFile.storage.path + "/" + outputFile.fileName)
        onCompleted: start();
    }

}