如何使用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

时间: 2024-08-09 23:52:41

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

在ASP.NET中,IE与Firefox下载文件带汉字名时乱码的解决方法

解决办法: HttpContext.Current.Response.Clear(); HttpContext.Current.Response.Buffer = true; HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); HttpContext.Current.Response.Charset = "gb2312"; HttpCon

2.1.5基础之命令行链接ftp dos中的ftp上传下载文件

Windows命令行batcmd脚本的应用之自动备份 异地备份2.1.5基础之命令行链接ftp dos中的ftp上传下载文件 讲解环境 VMware Workstation 12 桌面虚拟计算机软件创建虚拟机安装操作系统:http://edu.51cto.com/course/10007.html PC1:192.168.1.201 远程地址:192.168.100.100:2001 windows service2008 pc1 Admin111FTP虚拟用户 fileaa fileaaPC2

Java中实现FTP上传下载文件的功能,完整代码

一个JAVA 实现FTP功能的代码,包括了服务器的设置模块,并包括有上传文件至FTP的通用方法.下载文件的通用方法以及删除文件.在ftp服务器上穿件文件夹.检测文件夹是否存在等,里面的有些代码对编写JAVA文件上传或许有参考价值,Java FTP主文件代码: package ftpDemo;         import java.io.DataOutputStream;         import java.io.InputStream;         import java.io.Out

linux中使用lftp上传下载文件

lftp是linux中一款ftp服务器相比windows中的ftp显得要复杂不少了,下面我来总结一下lftp文件上传,文件下载,及文件查找等等相关命令吧. lftp连接的几种方法,最常用的是lftp [email protected],这样可以不用明文输入密码. 1.lftp [email protected] 回车 输入密码 2.lftp name:[email protected] 回车 3.lftp site 回车login 用户名 密码 4.lftp 回车 open site 回车 lo

在windows中使用xshell上传下载文件到linux中

使用centos的文件上传下载小工具,可以快速的帮助我们从本地上传文件至服务器,或者是从服务器下载文件至本地. 工具/原料 Centos xshell 方法/步骤 首先使用xshell 连接上服务器.新建一个连接,分别输入用户名和密码,再输入主机IP地址及端口号,选择ssh连接方式. 在连接属性中指定接收文件的文件夹 配置好连接属性之后连接上服务器 文件上传命令为rz,下载命令为sz,但是Linux可能没有安装该上传工具,所以输入rz和sz的时候会提示找不到该命令.所以需要需要使用命令安装下该工

Android SDK Manager 在下载文件的时候出现了Done. Nothing was installed.

解决方案: 在SDK Manager下Tools->Options打开了SDK Manager的Settings,选中"Force https://- sources to be fetched using http://-",强制使用http协议. Windows在C:\WINDOWS\system32\drivers\etc打开/etc/hosts文件,添加 #google_android更新 203.208.46.146 dl.google.com 203.208.46.1

在Ubuntu系统中解压rar和zip文件的方法

大家在以前的windows系统中会存有很多rar和zip格式的压缩文件,Ubuntu系统默认情况下对这些文件的支持不是很好,如果直接用"归档管理器"打开会提示错误,因此今天跟大家分享一下如何在Ubuntu中解压这两种格式的压缩文件: 我们需要安装两款软件:1.7zip:2.Ark,这两款软件在Ubuntu的软件中心都有下载: 打开软件中心搜索rar,在列表中找到它们,直接安装就可以- 安装完成后,就可以直接双击打开rar和zip格式的文件了,即使是中文格式也不会报错- 如果要解压,鼠标

ubuntu SSH 连接、远程上传下载文件

转自:http://www.cnblogs.com/by-1075324834/p/5045096.html 安装 SSH(Secure Shell) 服务以提供远程管理服务 sudo apt-get install ssh SSH 远程登入 Ubuntu 机 ssh [email protected] 将 文件/文件夹 从远程 Ubuntu 机拷至本地(scp) scp -r [email protected]:/home/username/remotefile.txt . 将 文件/文件夹

在MFC中通过访问IP地址下载文件到本地

void CDownLoad::OnBnClickedOk() { // TODO: 在此添加控件通知处理程序代码 CDialogEx::OnOK(); UpdateData(TRUE); CString sPath = m_savePath;//下载文件的保存地址 CString m_theUrl = m_ipPath;//下载的网址 CString filename = sPath + (_T("11.mdb"));//下载文件的保存名 CInternetSession sessi