Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
835 views
in Technique[技术] by (71.8m points)

c++ - Qt Download File - QNetworkAccessManager, not getting data

I'm trying to have my application download a file from a URL, typically an EXE or a Jar, not that this should change much though. I have this all running in a thread, but I don't think that will make a difference (if it does let me know).

So Do_Download is my function that creates the manager, sets the URL and request, and performs get. I then try to connect the finished signal to the slot the will write the file.

void DownloadManager::Do_Download() {
    QNetworkAccessManager *netManager = new QNetworkAccessManager(this);
    QUrl url(install_mirror); //istall_mirror is the URL provided by user
    QNetworkRequest req(url);

    QNetworkReply *reply = netManager->get(req);

    connect(reply, SIGNAL(finished()), this, SLOT(writeData()));
}

My writeData function checks for errors, and if there are no errors it writes the data to file.

void DownloadManager::writeData() {
    QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());

    if (reply) {
        if (reply->error() == QNetworkReply::NoError) {
            QFile file(location);
            if(file.open(QIODevice::WriteOnly)) {
                    file.write(reply->readAll());
            } else {
                errorMessage = "Error writing downloaded file for mirror installation";
            }
        } else {
            //get http status code
            int httpStatus = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
            errorMessage = "HTTP Error code while downloading from mirror: " + httpStatus;
        }

        reply->deleteLater();
    } else {
        errorMessage = "Error downloading file from installation mirror";
    }
}

The problem being there is no data being written. It just creates a 0Kb file. I tried adding a download progress slot so I could see what was going on recieving the data. So I added this to my Do_Download method.

connect(reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(DL_Progress(qint64,qint64)));

void DownloadManager::DL_Progress(qint64 recieved, qint64 total) {
    std::cout << recieved << " / " << total << endl;
}

The output displays one time as 0 / 01

What am I doing wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The only problem I see in your code is you are not waiting for the download to be finished. The NetworkRequest object would be destructed at the end of function call.

So, I would rewrite Do_Download like this (QEventLoop syncronizes the network request):

void DownloadManager::Do_Download() {
    QEventLoop eventLoop;
    QNetworkAccessManager *netManager = new QNetworkAccessManager(this);
    QUrl url(install_mirror); //istall_mirror is the URL provided by user
    QNetworkRequest req(url);

    QNetworkReply *reply = netManager->get(req);

    connect(reply, SIGNAL(finished()), &eventLoop, SLOT(quit()));
    eventLoop.exec();

    writeData(reply);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...