Files Damaged on Upload with QNetworkAcessManager

I am developing a client application whose purpose is to download some files from a web server, store them temporarily in the Temp folder, check the integrity of the file, and then send them to an FTP server on an embedded Linux device.

I recently had problems running a pre-development process while trying to boot from my local machine (see related question here ). Now I can upload and download without error and with validation (I use QCryptographicHash + file size to check for any inconsistencies) from my machine, but the same does not happen when I try to download from an HTTP server.

A total of 8 files are loaded: three .tar.gz, one plain text, two programs, and two binaries. I can successfully load all the compressed files, the text file and one of the binaries, but the others, namely two programs and one of the binaries (Linux kernel image) always load incorrectly.

I check this by not only seeing that the hash returns an error for them, but also for their sizes: their sizes are always wrong (and always the same error value for any download attempt). And the files on the server are correct.

My first suspicion was that I needed to configure the HTTP download in a way that is different from the general FTP download from the local machine, but I don't know how that would be. Moreover, if this additional configuration is necessary, then why are some files always returned correctly?

Here's the relevant code:

void MainWindow::processNextDownload()
{
    QUrl ulrTemp(updateData.at(transferStep).downloadUrl.arg(ui->leID->text()));    //"//" +

    if (updateData.at(transferStep).downloadUrl.contains("http"))
        ulrTemp.setScheme("http");
    else
        ulrTemp.setScheme("file");

//    ulrTemp.setUserName();
//    ulrTemp.setPassword();
//    ulrTemp.setPort();

    qDebug() << "Downloading" << transferStep << "from" << ulrTemp;

#if 1
    poReply = downloadNetworkManager->get(QNetworkRequest(ulrTemp));
#else
    QNetworkRequest request;
    request.setUrl(ulrTemp);
    request.setRawHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.60 Safari/537.17");

    poReply = downloadNetworkManager->get(request);
#endif

    connect(poReply,SIGNAL(downloadProgress(qint64,qint64)),this,SLOT(slotDownloadProgress(qint64,qint64)));

    ui->statusBar->showMessage(tr("Downloading: %1").arg(updateData.at(transferStep).itemName));
}


void MainWindow::slotFileDownloaded(QNetworkReply* reply)
{
    if (reply && (reply->error() != QNetworkReply::NoError))
    {
        ui->statusBar->clearMessage();

        if (!isSingleFile)
        {
            const QString strTemp = tr("An error occurred while downloading \"%1\": %2 (error message: %3)")
                                    .arg(updateData.at(transferStep).downloadName).arg(reply->error()).arg(reply->errorString());

            QMessageBox::critical(this,tr("Error in download"),strTemp);
        }
        else
        {
            //irrelevant...
        }

        finished(false);

        return;
    }

    qDebug() << "To write: " << reply->bytesAvailable();

    QByteArray downloadedData = reply->readAll();

    reply->deleteLater();
    poReply->deleteLater();

    //![]
    static const QString tempFilePath = QDir::tempPath();

    if (!isSingleFile)
    {
        QFile file(tempFilePath + "/" + updateData.at(transferStep).downloadName);


        if (!file.open(QFile::WriteOnly | QFile::Truncate))
        {
            qDebug() << "Failure opening temp file to write: " << file.fileName();

            QMessageBox::critical(this,
                                  tr("Error"),
                                  tr("An error occured while trying to open a temporary file to write the downloaded data (file: %1).").arg(file.fileName()));

            transferStep = downloadStepCount;

            slotFileUploaded(NULL);

            return;
        }

        qint32 bytesWritten = file.write(downloadedData);
        file.flush();

        if (downloadedData.size() != bytesWritten)
            qDebug() << "Write failed, wrote" << bytesWritten << "out of" << downloadedData.size() << "bytes.";
        else
            qDebug() << "Wrote " << bytesWritten << "bytes.";


        file.close();

        //![]
        if (++transferStep >= downloadStepCount)
        {
            qDebug() << "Start hash check";
            slotStartHashCheck();

            return;
        }

        processNextDownload();
    }
    else
    {
        //irrelevant...
    }
}

      

And this is why I get (the last value of the number after the "=" in the hash code is the file size):

Error message

So what am I missing?

+3


source to share





All Articles