Download Ftp File using NetworkAccessManager finishes before complete



  • Hi,
    i am trying to download file from ftp server using NetworkAccessManager but finished signal of NetworkAccessManager triggers before file download completes. This happens %90 of downloads. What is wrong with my code? Here is my Start Download Method:

     QNetworkRequest req(url);
        reply = nam->get(req);
    
        file.setFileName(_currentFileTryingToDownload);
        file.open(QIODevice::WriteOnly);
    
        connect(reply, SIGNAL(readyRead()), this,SLOT(readyRead()));
        connect(reply,SIGNAL(downloadProgress(qint64,qint64)),this,SLOT(DownloadProgress(qint64,qint64)));
        connect(nam, SIGNAL(finished(QNetworkReply*)),this,SLOT(downloadFinished(QNetworkReply*)));
        connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),this, SLOT(requestError(QNetworkReply::NetworkError)));
    

    this is ReadRead slot:

      QNetworkReply* content= qobject_cast<QNetworkReply*>(sender());
    
        if(content)
        {
             QByteArray data = content->readAll() ;
             qDebug() << "ReadyRead Bytes Count: " << data.length();
             file.write(data);
        }
    

    this is finished slot

     if (reply->error()) {
            qDebug() << "Download Finished Error: " << reply->error();
            file.close();
            file.remove();
            //todo screen msg and close
        } else {
            file.close();
            //todo unzip file
        }
    
        reply->deleteLater();
    

  • Qt Champions 2019

    @hakanaktan From https://doc.qt.io/qt-5/qnetworkreply.html#finished
    "Unless close() or abort() have been called, the reply will be still be opened for reading, so the data can be retrieved by calls to read() or readAll(). In particular, if no calls to read() were made as a result of readyRead(), a call to readAll() will retrieve the full contents in a QByteArray."
    In your finished slot check whether there is something to read and read if so.



  • When i try to read in slot downloadFinished, i get no bytes.
    here is my DownloadFinished slot and it's debug log

    void KullaniciMesajFtp::downloadFinished(QNetworkReply *reply)
    {
        qDebug() << "Download Finished : " << _currentFileTryingToDownload;
    
        if (reply->error()) {
            qDebug() << "Download Finished Error: " << reply->error();
            UpdatePBar(100,"Dosya indirme hatasi " + _currentFileTryingToDownload);
            file.close();
            file.remove();
    
            //todo screen msg and close
        } else {
    
            qDebug() << "Download Finished Close File";
    
            if(reply)
            {
                qDebug() << "reply->isFinished(): " << reply->isFinished();
                qDebug() << "reply->isReadable(): " << reply->isReadable();
    
                if(reply->isReadable())
                {
                    QByteArray data = reply->readAll() ;
                    qDebug() << "ReadyRead Bytes Count: " << data.length();
                    file.write(data);
                }
            }
    
            file.close();
            //todo unzip file
        }
    
        reply->deleteLater();
    }
    

    Here is the log :

    Download Finished : "MyFile.zip"
    Download Finished Close File
    reply->isFinished(): true
    reply->isReadable(): true
    ReadyRead Bytes Count: 0



  • @hakanaktan
    How do we (you) know that "finished signal of NetworkAccessManager triggers before file download completes", i.e. that it has correctly completed transferring the file?

    If that is indeed the case, does it terminate at the same place through the file(s), or do you lose the last block only, or do you lose content inside the file, or what?



  • @hakanaktan

    In your first post you have a connect to readyRead() that will be triggered most likely several times. If this is still there that might be the explanation that no data is available when finished is triggered. I suggest that you either way and the better is wait for finished signal.


Log in to reply