Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Download Ftp File using NetworkAccessManager finishes before complete

Download Ftp File using NetworkAccessManager finishes before complete

Scheduled Pinned Locked Moved Unsolved General and Desktop
ftpdownloadqt5.5
5 Posts 4 Posters 1.1k Views
  • 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 Offline
    H Offline
    hakanaktan
    wrote on 7 Aug 2019, 07:26 last edited by
    #1

    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();
    
    J J 2 Replies Last reply 7 Aug 2019, 08:07
    0
    • H hakanaktan
      7 Aug 2019, 07:26

      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();
      
      J Online
      J Online
      jsulm
      Lifetime Qt Champion
      wrote on 7 Aug 2019, 08:07 last edited by
      #2

      @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
      3
      • H Offline
        H Offline
        hakanaktan
        wrote on 7 Aug 2019, 08:20 last edited by
        #3

        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 7 Aug 2019, 19:26
        0
        • H hakanaktan
          7 Aug 2019, 07:26

          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();
          
          J Offline
          J Offline
          JonB
          wrote on 7 Aug 2019, 19:20 last edited by
          #4

          @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
          0
          • H hakanaktan
            7 Aug 2019, 08:20

            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 Offline
            K Offline
            koahnig
            wrote on 7 Aug 2019, 19:26 last edited by
            #5

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

            1/5

            7 Aug 2019, 07:26

            • Login

            • Login or register to search.
            1 out of 5
            • First post
              1/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved