如何使用Ubuntu SDK中的Download Manager来下载文件

对于一下应用来说,我们需要使用网路上的一下文件,并下载它们。那么我们怎么在QML应用中来下载文件呢?我们在SDK API的网页中,我们发现有一个叫做Download Manager的API。我们可以使用SingleDownloadDownloadManager来下载一个或多个文件。


首先,我们来创建一个简单的Download应用。这里我们使用“QML app with Simple UI (qmlproject)”。对于大家不熟悉Download Manager的开发者来说,我们可以使用如下的方法得到它更加详细的接口:


$qmlplugindump Ubuntu.DownloadManager 0.1

通过上面的命令,我们可以得到如下的输出:


import QtQuick.tooling 1.1

// This file describes the plugin-supplied types contained in the library.
// It is used for QML tooling purposes only.
//
// This file was auto-generated by:
// 'qmlplugindump Ubuntu.DownloadManager 0.1'

Module {
    Component {
        name: "Ubuntu::DownloadManager::DownloadError"
        prototype: "QObject"
        exports: ["Error 0.1"]
        exportMetaObjectRevisions: [0]
        Property { name: "type"; type: "string"; isReadonly: true }
        Property { name: "message"; type: "string"; isReadonly: true }
    }
    Component {
        name: "Ubuntu::DownloadManager::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 { name: "extract"; type: "bool" }
        Signal { name: "showIndicatorChanged" }
    }
    Component {
        name: "Ubuntu::DownloadManager::SingleDownload"
        prototype: "QObject"
        exports: ["SingleDownload 0.1"]
        exportMetaObjectRevisions: [0]
        Property { name: "autoStart"; type: "bool" }
        Property { name: "errorMessage"; type: "string"; isReadonly: true }
        Property { name: "isCompleted"; type: "bool"; isReadonly: true }
        Property { name: "downloadInProgress"; 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: "headers"; type: "QVariantMap" }
        Property { name: "metadata"; type: "Ubuntu::DownloadManager::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"; type: "qulonglong" }
        }
        Signal {
            name: "resumed"
            Parameter { name: "success"; type: "bool" }
        }
        Signal {
            name: "started"
            Parameter { name: "success"; type: "bool" }
        }
        Signal {
            name: "errorFound"
            Parameter { name: "error"; type: "DownloadError&" }
        }
        Signal { name: "errorChanged" }
        Method {
            name: "registerError"
            Parameter { name: "error"; type: "Error"; isPointer: true }
        }
        Method {
            name: "bindDownload"
            Parameter { name: "download"; type: "Download"; isPointer: true }
        }
        Method {
            name: "unbindDownload"
            Parameter { name: "download"; type: "Download"; isPointer: true }
        }
        Method {
            name: "onFinished"
            Parameter { name: "path"; type: "string" }
        }
        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: "string" }
        }
    }
    Component {
        name: "Ubuntu::DownloadManager::UbuntuDownloadManager"
        prototype: "QObject"
        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: "errorChanged" }
        Method {
            name: "download"
            Parameter { name: "url"; type: "string" }
        }
    }
}

在今天的例子里,我们将使用SingleDownload来下载我们所需要的图片。我们可以看到,SingleDownload有“finished”信号,我们可以通过这个信号来得到文件下载完毕的通知,并在“path”中得到我们所下载文件的路径。当然我们也可以使用其它的信号来得到下载的更多的信息。


我们来修改我们的“Main.qml”如下:


import QtQuick 2.0
import Ubuntu.Components 1.1
import 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"

    /*
     This property 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(50)
    height: units.gu(75)

    Page {
        title: i18n.tr("Download")

        Rectangle {
            width: parent.width
            height: units.gu(20)
            TextField {
                id: text
                placeholderText: "File URL to download..."
                height: units.gu(5)
                anchors {
                    left: parent.left
                    right: button.left
                    rightMargin: units.gu(2)
                }

                text: "http://bbs.unpcn.com/attachment.aspx?attachmentid=3820584"
            }

            Button {
                id: button
                text: "Download"
                height: 50
                anchors.right: parent.right
                anchors.verticalCenter: text.verticalCenter
                onClicked: {
                    single.download(text.text);
                }
            }

            TextField {
                id: downloaded
                placeholderText: "Downloaded file address"
                height: 100
                anchors {
                    left: parent.left
                    right: parent.right
                    top: button.bottom
                    rightMargin: units.gu(2)
                    topMargin: units.gu(1)
                }
            }

            ProgressBar {
                id: progress
                minimumValue: 0
                maximumValue: 100
                value: single.progress
                anchors {
                    left: parent.left
                    right: parent.right
                    bottom: parent.bottom
                }

                SingleDownload {
                    id: single

                    onFinished: {
                        downloaded.text = path;
                        console.log("downloaded path: " + path);
                    }
                }
            }

            Image {
                anchors.top: progress.bottom
                source: downloaded.text
            }
        }
    }
}


界面非常简单,我们在上面可以输入我们想要的URL文件地址,在下面显示下载后的文件的路径。我们可以点击“Download”按钮来下载我们所需要的文件。为了说明问题,我们在下面使用了一个Image来显示下载的图片:


技术分享


所有的源码在:git clone https://gitcafe.com/ubuntu/download.git


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。