For the application, we need to use the files on the Internet and download them. So how do we download files in the Qml app? We found an API called Download Manager in the SDK API page. We can use Singledownload or Downloadmanager to download one or more files.
First, let's create a simple download application. Here we use the QML app with the simple UI (qmlproject). For developers who are unfamiliar with Download Manager, we can use the following method to get a more detailed interface:
$qmlplugindump Ubuntu.downloadmanager 0.1
With the above command, we can get the following output:
Import qtquick.tooling 1.1//This file describes the Plugin-supplied types contained in the library.//It's used for QML Tooling purposes only.////This file is auto-generated by://' qmlplugindump ubuntu.downloadmanager 0.1 ' Module {Compon ENT {name: "Ubuntu::D ownloadmanager::D ownloaderror" prototype: "Qobject" exports: ["Error 0.1"] Exportmetaobjectrevisions: [0] Property {name: ' type '; Type: ' String '; Isreadonly:true} property {Nam E: "Message"; Type: "string"; Isreadonly:true}} Component {name: "Ubuntu::D ownloadmanager::metadata" prototype: "Qobject" Exports: ["Metadata 0.1"] Exportmetaobjectrevisions: [0] Property {name: "title"; Type: "String"} Property {name: ' Showinindicator '; Type: ' BOOL '} property {name: ' deflate '; Type: ' BOOL '} property {Nam E: "Extract"; Type: "BOOL"} Signal {name: "Showindicatorchanged"}} Component {name: "Ubuntu::D ownloadmanager::singledownload "prototype:" Qobject "exports: [" Singledownload 0.1 "] Exportmetaobj Ectrevisions: [0] Property {name: ' AutoStart '; Type: ' BOOL '} property {name: ' errormessage '; Type: "Strin G "; Isreadonly:true} Property {name: ' iscompleted '; Type: "bool"; Isreadonly:true} property {name: "Downlo Adinprogress "; Type: "BOOL"; Isreadonly:true} Property {name: ' Allowmobiledownload '; Type: ' BOOL '} property {name: ' throttle '; Type: "Qulonglong"} Property {Name: "Progress"; Type: "int", isreadonly:true} property {name: "Downloading"; Type: "BOOL"; Isreadonly:true} Property {name: ' Downloadid '; Type: ' String '; Isreadonly:true} property {name: ' Heade RS "; Type: "Qvariantmap"} Property {name: "metadata"; Type: "Ubuntu::D ownloadmanager::metadata"; Ispointer:true} Signal {name: "Canceled" Parameter {name: "Success"; Type: "BOOL" }} Signal {name: "finished" Parameter {name: "path"; Type: "String"}} Signal {name: "paused" Parameter {name: "Success"; Type: "bool"}} Signal { Name: "Processing" Parameter {name: "path"; Type: "string"}} Signal {name: "Progressreceived" Parameter {name: "Received"; Type: "Qulonglong"} Parameter {name: "Total"; Ty PE: "Qulonglong"}} Signal {name: "resumed" Parameter {name: "Success"; Type: "bool "}} Signal {name:" Started "Parameter {name:" Success "; Type: "bool"}} Signal {name: "Errorfound" Parameter {name: "error"; Type: "Downloa derror& "}} Signal {name:" errorchanged "} Method {name:" Registererror " Parameter {name: "error"; Type: "Error"; ispointEr:true}} Method {name: "Binddownload" Parameter {name: "Download"; Type: "Downlo Ad "; Ispointer:true}} Method {name: "Unbinddownload" Parameter {name: "Download"; type : "Download"; Ispointer:true}} Method {name: "onfinished" Parameter {name: "path"; Type: "Strin G "}} Method {name:" OnProgress "Parameter {name:" Received "; Type:" Qulonglong "} Parameter {name: "Total"; Type: "Qulonglong"}} Method {name: "onpaused" Parameter {name: "waspaused"; Type: "bool"}} Method {name: "onresumed" Parameter {Name: "wasresumed"; Type: "BOOL"}} Method {Name: "onstarted" Parameter {name: "wasstarted"; Type: "bool"}} Method { Name: "oncanceled" Parameter {name: "wascanceled"; type: "bool"}} method {name: "Start"} method {name: "Pause"} method {name: "Resume"} Method {Name: "Cancel"} method {name: "Download" Parameter {name: "url"; Type: "Stri Ng "}}} Component {name:" Ubuntu::D Ownloadmanager::ubuntudownloadmanager "prototype:" Qobjec T "exports: [" Downloadmanager 0.1 "] Exportmetaobjectrevisions: [0] Property {name:" AutoStart "; type : "BOOL"} Property {name: "Cleandownloads"; Type: ' BOOL '} property {name: "ErrorMessage"; Type: "String" ; Isreadonly:true} Property {name: "Downloads"; Type: "Qvariantlist"; Isreadonly:true} Signal {name: "ER Rorchanged "} Method {name:" Download "Parameter {name:" url "; Type:" String "}} }}
In today's example, we will use Singledownload to download the images we need. We can see that the singledownload has "finished" signal, we can get the file download complete notification, and in "path" to get the path of the file we downloaded. Of course we can also use other signals to get more information on the download.
We will revise our "MAIN.QML" as follows:
Import QtQuick 2.0import ubuntu.components 1.1import ubuntu.downloadmanager 0.1/*! \brief MainView with a Label and Button Elements.*/mainview {//ObjectName for functional testing purposes (autopilot- QT5) ObjectName: "MainView"//note! ApplicationName needs to match the "name" field of the click Manifest ApplicationName: "Download.liu-xiao-guo"/* The enables the application to change orientation when the device is rotated. The default is False. *///automaticorientation:true//Removes the old toolbar and enables new features of the new header. Usedeprecatedtoolbar:false width:units.gu (height:units.gu) page {title:i18n.tr ("Download") Rectangle {width:parent.width Height:units.gu (TextField) {id:t Ext placeholdertext: "File URL to download ..." Height:units.gu (5) Anchors { Left:parenT.left right:button.left RightMargin:units.gu (2)} t Ext: "http://bbs.unpcn.com/attachment.aspx?attachmentid=3820584"} button {Id:butto N Text: "Download" height:50 anchors.right:parent.right anch Ors.verticalCenter:text.verticalCenter onclicked: {single.download (Text.text); }} TextField {id:downloaded placeholdertext: "Downloaded F Ile Address "height:100 anchors {left:parent.left R Ight:parent.right Top:button.bottom RightMargin:units.gu (2) to PMargin:units.gu (1)}} ProgressBar {id:progress min imumvalue:0 maximumvalue:100 value:single.progress Anchors {left:parent.left Right:parent.right Bottom:parent.bottom} singledownl oad {id:single onfinished: {downloaded.text = path; Console.log ("Downloaded path:" + path); }}} Image {Anchors.top:progress.bottom Source:down Loaded.text}}}}
The interface is very simple, we can enter the URL we want to file the address, the following shows the path of the downloaded file. We can click on the "Download" button to download the files we need. To illustrate the problem, we used an image below to display the downloaded image:
All the source code in: Git clone https://gitcafe.com/ubuntu/download.git
How to use Download Manager in the Ubuntu SDK to download files