Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved Download Ftp File using NetworkAccessManager finishes before complete

    General and Desktop
    ftp download qt5.5
    4
    5
    535
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • H
      hakanaktan last edited by

      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();
      
      jsulm JonB 2 Replies Last reply Reply Quote 0
      • jsulm
        jsulm Lifetime Qt Champion @hakanaktan last edited by

        @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.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply Reply Quote 3
        • H
          hakanaktan last edited by

          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

          K 1 Reply Last reply Reply Quote 0
          • JonB
            JonB @hakanaktan last edited by

            @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?

            1 Reply Last reply Reply Quote 0
            • K
              koahnig @hakanaktan last edited by

              @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.

              Vote the answer(s) that helped you to solve your issue(s)

              1 Reply Last reply Reply Quote 0
              • First post
                Last post