Downloading file using URL in QT



  • Hi,

    All i want to download a file using url in QT and return content as char *.

    i used below code. download is success and content written in a file.

    /****************************************************************************
    **
    ** Copyright (C) 2016 The Qt Company Ltd.
    ** Contact: https://www.qt.io/licensing/
    **
    ** This file is part of the examples of the Qt Toolkit.
    **
    ** $QT_BEGIN_LICENSE:BSD$
    ** Commercial License Usage
    ** Licensees holding valid commercial Qt licenses may use this file in
    ** accordance with the commercial license agreement provided with the
    ** Software or, alternatively, in accordance with the terms contained in
    ** a written agreement between you and The Qt Company. For licensing terms
    ** and conditions see https://www.qt.io/terms-conditions. For further
    ** information use the contact form at https://www.qt.io/contact-us.
    **
    ** BSD License Usage
    ** Alternatively, you may use this file under the terms of the BSD license
    ** as follows:
    **
    ** "Redistribution and use in source and binary forms, with or without
    ** modification, are permitted provided that the following conditions are
    ** met:
    ** * Redistributions of source code must retain the above copyright
    ** notice, this list of conditions and the following disclaimer.
    ** * Redistributions in binary form must reproduce the above copyright
    ** notice, this list of conditions and the following disclaimer in
    ** the documentation and/or other materials provided with the
    ** distribution.
    ** * Neither the name of The Qt Company Ltd nor the names of its
    ** contributors may be used to endorse or promote products derived
    ** from this software without specific prior written permission.
    **
    **
    ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
    **
    ** $QT_END_LICENSE$
    **
    ****************************************************************************/

    #include <QObject>
    #include <QString>
    #include <QtNetwork/QNetworkAccessManager>
    #include <QtNetwork/QNetworkReply>

    class QtDownload : public QObject {
    Q_OBJECT
    public:
    explicit QtDownload();
    ~QtDownload();

    void setTarget(const QString& t);
    

    private:
    QNetworkAccessManager manager;
    QString target;

    signals:
    void done();

    public slots:
    void download();
    void downloadFinished(QNetworkReply* data);
    void downloadProgress(qint64 recieved, qint64 total);
    };
    #include <QCoreApplication>
    #include <QUrl>
    #include <QNetworkRequest>
    #include <QFile>
    #include <QDebug>

    QtDownload::QtDownload() : QObject(0) {
    QObject::connect(&manager, SIGNAL(finished(QNetworkReply*)),this, SLOT(downloadFinished(QNetworkReply*)));
    }

    QtDownload::~QtDownload() {

    }

    void QtDownload::setTarget(const QString &t) {
    this->target = t;
    }

    void QtDownload::downloadFinished(QNetworkReply *data) {
    QFile localFile("/home/roi/Desktop/downloadedfile.txt");
    if (!localFile.open(QIODevice::WriteOnly))
    return;
    const QByteArray sdata = data->readAll();
    localFile.write(sdata);
    qDebug() << sdata;
    localFile.close();

    emit done();
    

    }

    void QtDownload::download() {
    QUrl url = QUrl::fromEncoded(this->target.toLocal8Bit());
    QNetworkRequest request(url);
    QObject::connect(manager.get(request), SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(downloadProgress(qint64,qint64)));

    }

    void QtDownload::downloadProgress(qint64 recieved, qint64 total) {
    qDebug() << recieved << total;
    }
    #include <QtCore>
    int main(int argc, char **argv) {
    QCoreApplication app(argc, argv);
    QtDownload dl;
    dl.setTarget("http://guest:p2guest@192.168.55.3/contents/2/clip/0711WH.xml");

    dl.download();
    //quit when the download is done.
    QObject::connect(&dl, SIGNAL(done()), &app, SLOT(quit()));
    return app.exec();
    

    // return 0;
    }
    #include "main.moc"

    but my requirement is download file return its content as char *.
    and 2nd thing is

    i am using it from a fun named as

    char * downloadfromUrl(std::wstring szUrl)
    {
    QString Url= QString::fromStdWString(szUrl);
    QtDownload dl;
    //dl.setTarget("http://guest:p2guest@192.168.55.3/contents/2/clip/0711WH.xml");
    dl.setTarget(Url);

    dl.download();

    }

    its not complete.

    please help me for completing downloadfromUrl fun as well as downloadFinished().

    it will be great help, please suggest me.

    signature of downloadfromUrl is char *.

    means downloaded content should be return as char *.

    Thanks
    Shashi


  • Lifetime Qt Champion

    Hi,

    Then add a getter function that returns a char pointer to the start of the buffer you used to store the downloaded file.



  • Thanks Sgaist,

    but how i will call. i do not have main(). i have to call it from
    ownloadfromUrl(std::wstring szUrl)
    {

    }

    i do not have
    nt main(int argc, char **argv) {
    QCoreApplication app(argc, argv);



    }

    please suggest.

    Thanks
    shashi


  • Lifetime Qt Champion

    Before any suggestion, you should describe your application more precisely.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.