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. problem to download 2 files one after the other with QNetworkAccesManager

problem to download 2 files one after the other with QNetworkAccesManager

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 4 Posters 691 Views 2 Watching
  • 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.
  • C Offline
    C Offline
    cosmoff
    wrote on last edited by
    #1

    Hello,

    I use the class QNetworkAccesManager to download files.

    I created two buttons, and when I click on one button, I download a file, when I click on another button I download another file.

    if I click on the first button, the downloading works very well. If I quit the program and run back the program, and if I click on the second button,
    it works as well.

    but if I run the program, I click on the first button, the downloading works, but if I click on the second button it doesn't work and I do not why. I can not download
    2 files one after the other.

    this is my code:

    QString _url = "ftp://username:password@192.168.0.10/file.zip" ;
    QNetworkRequest request ;
    request.setUrl ( _url ) ;
    QNetworkReply* reply = m_networkAccessManager.get ( request );
    
    connect ( reply, &QNetworkReply::downloadProgress, this, SLOT( onDownloadProgress ));
    connect ( reply, &QNetworkReply::finished, this, SLOT( onFinished )) ;
    

    thanks for your help

    jsulmJ 1 Reply Last reply
    0
    • C cosmoff

      Hello,

      I use the class QNetworkAccesManager to download files.

      I created two buttons, and when I click on one button, I download a file, when I click on another button I download another file.

      if I click on the first button, the downloading works very well. If I quit the program and run back the program, and if I click on the second button,
      it works as well.

      but if I run the program, I click on the first button, the downloading works, but if I click on the second button it doesn't work and I do not why. I can not download
      2 files one after the other.

      this is my code:

      QString _url = "ftp://username:password@192.168.0.10/file.zip" ;
      QNetworkRequest request ;
      request.setUrl ( _url ) ;
      QNetworkReply* reply = m_networkAccessManager.get ( request );
      
      connect ( reply, &QNetworkReply::downloadProgress, this, SLOT( onDownloadProgress ));
      connect ( reply, &QNetworkReply::finished, this, SLOT( onFinished )) ;
      

      thanks for your help

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @cosmoff Can you show the button slot where you start the download?
      Do you use same request object?
      And what does "doesn't work" mean exactly?

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

      1 Reply Last reply
      2
      • C Offline
        C Offline
        cosmoff
        wrote on last edited by cosmoff
        #3

        thanks for your answer.

        this the code of the button :

        void boutonDownloadSfs()
        {
            m_serverInterface->onRequestDownloading("file.zip");
        }
        

        Yes I use the same request object which is m_serverInterface :

        ServerInterface::onRequestDownloading( const QString& path ){
        	QNetworkRequest request ;
        	QString _url = "ftp://username:passsword@192.168.0.10" + "/" + path;
            request.setUrl ( _url ) ;
            QNetworkReply* reply = m_networkAccessManager.get ( request );
        
            connect ( reply, &QNetworkReply::downloadProgress, this, &PackageServerInterface::onDownloadProgress );
            connect ( reply, &QNetworkReply::finished, this, &PackageServerInterface::onFinished ) ;
        }
        

        work means I downloaded correctly the file on my computer

        jsulmJ 1 Reply Last reply
        0
        • C cosmoff

          thanks for your answer.

          this the code of the button :

          void boutonDownloadSfs()
          {
              m_serverInterface->onRequestDownloading("file.zip");
          }
          

          Yes I use the same request object which is m_serverInterface :

          ServerInterface::onRequestDownloading( const QString& path ){
          	QNetworkRequest request ;
          	QString _url = "ftp://username:passsword@192.168.0.10" + "/" + path;
              request.setUrl ( _url ) ;
              QNetworkReply* reply = m_networkAccessManager.get ( request );
          
              connect ( reply, &QNetworkReply::downloadProgress, this, &PackageServerInterface::onDownloadProgress );
              connect ( reply, &QNetworkReply::finished, this, &PackageServerInterface::onFinished ) ;
          }
          

          work means I downloaded correctly the file on my computer

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @cosmoff No, you don't use same object as it is local variable. But this should be fine.
          "work means I downloaded correctly the file on my computer" - this is clear and was not my question.
          The question is: what exactly does not work?

          Can you show onDownloadProgress and onFinished?

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

          1 Reply Last reply
          0
          • C Offline
            C Offline
            cosmoff
            wrote on last edited by cosmoff
            #5

            what exactly does not work : in fact, It downloads the second file but the size is null or It does not download the file in entire.

            onDownloadProgress :

            void ServerInterface::onDownloadProgress ( qint64 _bytesReceived, qint64 _bytesTotal )
            {
                if ( m_requestPending )
                {
                    QNetworkReply* networkReply = static_cast<QNetworkReply*> ( sender() ) ;
            
                    if ( networkReply != nullptr )
                    {
                        /* on lit toutes les données disponibles */
                        QByteArray data = networkReply->readAll() ;
            
                        if ( ! data.isEmpty() )
                        {
                            /* à la première reception */
                            if ( ! m_pendingFile.isOpen() )
                            {
                                m_bytesDownloaded = 0 ;
            
                                /* on retrouve le nom du fichier à partir
                                 * de la requête */
                                QNetworkRequest request = networkReply->request() ;
            
                                /* le nom est le dernier élément de l'url */
                                QStringList urlParts = request.url().path().split ( "/" ) ;
                                QString path = request.url().path() ;
            
                                QString fileName ;
            
                                if ( urlParts.size() > 0 )
                                {
                                    fileName = urlParts.last() ;
                                }
                                else
                                {
                                    m_requestPending = false ;
                                    m_pendingRequestType = RequestType::Undefined ;
                                    emit softwareVersionDownloadFinished ( false, "" ) ;
            
                                    LOGGER->debug ( "! urlParts.size() > 0", DETAILS ) ;
                                }
            
                                /* on crée le fichier */
                                m_pendingFile.setFileName ( QString ( "/tmp/" ) + fileName ) ;
                                m_pendingFile.open ( QIODevice::WriteOnly ) ;
                            }
            
                            /* on ajoute les données au fichier courant */
                            QDataStream out ( &m_pendingFile ) ;
            
                            qint64 dataSize = static_cast<qint64> ( data.size() ) ;
                            qint64 dataSizeToAdd = _bytesTotal - m_bytesDownloaded ;
            
                            /* si on a reçu trop de données,
                                    * on ne prend que celles utiles */
                            if ( dataSizeToAdd < dataSize )
                            {
                                /* on n'utilise pas l'opérateur << car il ajoute des fin de lignes ( '\0' ) aux données */
                                out.writeRawData ( data.left ( static_cast<int> ( dataSizeToAdd ) ).data(), data.left ( static_cast<int> ( dataSizeToAdd ) ).size() ) ;
                                m_bytesDownloaded += dataSizeToAdd ;
                            }
                            /* sinon on prend tout */
                            else
                            {
                                /* on n'utilise pas l'opérateur << car il ajoute des fin de lignes ( '\0' ) aux données */
                                out.writeRawData ( data.data(), data.size() ) ;
                                m_bytesDownloaded += dataSize  ;
                            }
            
                            if ( _bytesTotal > 0 )
                            {
                                int progress = static_cast<int> ( ( _bytesReceived * 100 ) / _bytesTotal ) ;
            
                                /* suivant le type de requête en attente de
                                * cette réponse */
                                switch ( m_pendingRequestType )
                                {
            
                                    /* demande de version */
                                    case RequestType::GetSoftwareVersion :
                                    {
                                        /* on signale la progression */
                                        emit softwareVersionDownloadProgress ( progress ) ;
                                    }
                                    break ;
            }
            }
            }
            

            and onFinished :

            void ServerInterface::onFinished()
            {
                qDebug()<<"processus bien terminé"<<endl;
                if ( m_requestPending )
                {
                    /* suivant le type de requête en attente de
                     * cette réponse */
                    switch ( m_pendingRequestType )
                    {
                        /* demande de ghost */
                        case RequestType::GetSoftwareVersion :
                        {
                            /* quand la réception est terminée on ferme
                                     * le fichier */
                            m_pendingFile.close() ;
            
                            m_requestPending = false ;
                            m_pendingRequestType = RequestType::Undefined ;
                            emit softwareVersionDownloadFinished ( true, m_pendingFile.fileName() ) ;
                        }
                        break ;
            }}
            
            jsulmJ 1 Reply Last reply
            0
            • C cosmoff

              what exactly does not work : in fact, It downloads the second file but the size is null or It does not download the file in entire.

              onDownloadProgress :

              void ServerInterface::onDownloadProgress ( qint64 _bytesReceived, qint64 _bytesTotal )
              {
                  if ( m_requestPending )
                  {
                      QNetworkReply* networkReply = static_cast<QNetworkReply*> ( sender() ) ;
              
                      if ( networkReply != nullptr )
                      {
                          /* on lit toutes les données disponibles */
                          QByteArray data = networkReply->readAll() ;
              
                          if ( ! data.isEmpty() )
                          {
                              /* à la première reception */
                              if ( ! m_pendingFile.isOpen() )
                              {
                                  m_bytesDownloaded = 0 ;
              
                                  /* on retrouve le nom du fichier à partir
                                   * de la requête */
                                  QNetworkRequest request = networkReply->request() ;
              
                                  /* le nom est le dernier élément de l'url */
                                  QStringList urlParts = request.url().path().split ( "/" ) ;
                                  QString path = request.url().path() ;
              
                                  QString fileName ;
              
                                  if ( urlParts.size() > 0 )
                                  {
                                      fileName = urlParts.last() ;
                                  }
                                  else
                                  {
                                      m_requestPending = false ;
                                      m_pendingRequestType = RequestType::Undefined ;
                                      emit softwareVersionDownloadFinished ( false, "" ) ;
              
                                      LOGGER->debug ( "! urlParts.size() > 0", DETAILS ) ;
                                  }
              
                                  /* on crée le fichier */
                                  m_pendingFile.setFileName ( QString ( "/tmp/" ) + fileName ) ;
                                  m_pendingFile.open ( QIODevice::WriteOnly ) ;
                              }
              
                              /* on ajoute les données au fichier courant */
                              QDataStream out ( &m_pendingFile ) ;
              
                              qint64 dataSize = static_cast<qint64> ( data.size() ) ;
                              qint64 dataSizeToAdd = _bytesTotal - m_bytesDownloaded ;
              
                              /* si on a reçu trop de données,
                                      * on ne prend que celles utiles */
                              if ( dataSizeToAdd < dataSize )
                              {
                                  /* on n'utilise pas l'opérateur << car il ajoute des fin de lignes ( '\0' ) aux données */
                                  out.writeRawData ( data.left ( static_cast<int> ( dataSizeToAdd ) ).data(), data.left ( static_cast<int> ( dataSizeToAdd ) ).size() ) ;
                                  m_bytesDownloaded += dataSizeToAdd ;
                              }
                              /* sinon on prend tout */
                              else
                              {
                                  /* on n'utilise pas l'opérateur << car il ajoute des fin de lignes ( '\0' ) aux données */
                                  out.writeRawData ( data.data(), data.size() ) ;
                                  m_bytesDownloaded += dataSize  ;
                              }
              
                              if ( _bytesTotal > 0 )
                              {
                                  int progress = static_cast<int> ( ( _bytesReceived * 100 ) / _bytesTotal ) ;
              
                                  /* suivant le type de requête en attente de
                                  * cette réponse */
                                  switch ( m_pendingRequestType )
                                  {
              
                                      /* demande de version */
                                      case RequestType::GetSoftwareVersion :
                                      {
                                          /* on signale la progression */
                                          emit softwareVersionDownloadProgress ( progress ) ;
                                      }
                                      break ;
              }
              }
              }
              

              and onFinished :

              void ServerInterface::onFinished()
              {
                  qDebug()<<"processus bien terminé"<<endl;
                  if ( m_requestPending )
                  {
                      /* suivant le type de requête en attente de
                       * cette réponse */
                      switch ( m_pendingRequestType )
                      {
                          /* demande de ghost */
                          case RequestType::GetSoftwareVersion :
                          {
                              /* quand la réception est terminée on ferme
                                       * le fichier */
                              m_pendingFile.close() ;
              
                              m_requestPending = false ;
                              m_pendingRequestType = RequestType::Undefined ;
                              emit softwareVersionDownloadFinished ( true, m_pendingFile.fileName() ) ;
                          }
                          break ;
              }}
              
              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @cosmoff I don't see you closing the file. Also you can add some qDebug() output to your code to see what happens.
              And it looks like you're trying to download same file two times: is that correct?

              void boutonDownloadSfs()
              {
                  m_serverInterface->onRequestDownloading("file.zip");
              }
              

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

              1 Reply Last reply
              1
              • VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by
                #7

                your onDownloadProgress is overcomplicated for what you are doing but the real problem is that the code should not respond to that signal but rather to QNetworkReply::readyRead()

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                1 Reply Last reply
                3
                • C Offline
                  C Offline
                  cosmoff
                  wrote on last edited by
                  #8

                  I download never 2 files in same time.
                  I think that the problem comes from the function onDownloadProgress because when I download the second file, I always enter in the line :

                  if ( dataSizeToAdd < dataSize )
                                  {
                                      qDebug() << "on a recu trop de data ..." <<endl;
                                      /* on n'utilise pas l'opérateur << car il ajoute des fin de lignes ( '\0' ) aux données */
                                      out.writeRawData ( data.left ( static_cast<int> ( dataSizeToAdd ) ).data(), data.left ( static_cast<int> ( dataSizeToAdd ) ).size() ) ;
                                      m_bytesDownloaded += dataSizeToAdd ;
                                  }
                  

                  do you have an idea ?

                  C 1 Reply Last reply
                  0
                  • C cosmoff

                    I download never 2 files in same time.
                    I think that the problem comes from the function onDownloadProgress because when I download the second file, I always enter in the line :

                    if ( dataSizeToAdd < dataSize )
                                    {
                                        qDebug() << "on a recu trop de data ..." <<endl;
                                        /* on n'utilise pas l'opérateur << car il ajoute des fin de lignes ( '\0' ) aux données */
                                        out.writeRawData ( data.left ( static_cast<int> ( dataSizeToAdd ) ).data(), data.left ( static_cast<int> ( dataSizeToAdd ) ).size() ) ;
                                        m_bytesDownloaded += dataSizeToAdd ;
                                    }
                    

                    do you have an idea ?

                    C Offline
                    C Offline
                    cosmoff
                    wrote on last edited by
                    #9

                    problem resolve !
                    I remplace the lines :

                    if ( dataSizeToAdd < dataSize )
                                    {
                                        qDebug() << "on a recu trop de data ..." <<endl;
                                        /* on n'utilise pas l'opérateur << car il ajoute des fin de lignes ( '\0' ) aux données */
                                        out.writeRawData ( data.left ( static_cast<int> ( dataSizeToAdd ) ).data(), data.left ( static_cast<int> ( taSizeToAdd ) ).size() ) ;
                                        m_bytesDownloaded += dataSizeToAdd ;
                                    }
                                    /* sinon on prend tout */
                                    else
                                    {
                                        qDebug() << "Le compte est bon." <<endl;
                                        /* on n'utilise pas l'opérateur << car il ajoute des fin de lignes ( '\0' ) aux données */
                                        out.writeRawData ( data.data(), data.size() ) ;
                                        m_bytesDownloaded += dataSize  ;
                                    }
                    
                    by:
                    

                    out.writeRawData ( data.data(), data.size() ) ;
                    m_bytesDownloaded += dataSize ;

                    thanks a lot for your help !
                    Pablo J. RoginaP 1 Reply Last reply
                    0
                    • C cosmoff

                      problem resolve !
                      I remplace the lines :

                      if ( dataSizeToAdd < dataSize )
                                      {
                                          qDebug() << "on a recu trop de data ..." <<endl;
                                          /* on n'utilise pas l'opérateur << car il ajoute des fin de lignes ( '\0' ) aux données */
                                          out.writeRawData ( data.left ( static_cast<int> ( dataSizeToAdd ) ).data(), data.left ( static_cast<int> ( taSizeToAdd ) ).size() ) ;
                                          m_bytesDownloaded += dataSizeToAdd ;
                                      }
                                      /* sinon on prend tout */
                                      else
                                      {
                                          qDebug() << "Le compte est bon." <<endl;
                                          /* on n'utilise pas l'opérateur << car il ajoute des fin de lignes ( '\0' ) aux données */
                                          out.writeRawData ( data.data(), data.size() ) ;
                                          m_bytesDownloaded += dataSize  ;
                                      }
                      
                      by:
                      

                      out.writeRawData ( data.data(), data.size() ) ;
                      m_bytesDownloaded += dataSize ;

                      thanks a lot for your help !
                      Pablo J. RoginaP Offline
                      Pablo J. RoginaP Offline
                      Pablo J. Rogina
                      wrote on last edited by
                      #10

                      @cosmoff please don't forget to mark your post as solved if you're done. Thanks.

                      Upvote the answer(s) that helped you solve the issue
                      Use "Topic Tools" button to mark your post as Solved
                      Add screenshots via postimage.org
                      Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                      1 Reply Last reply
                      0

                      • Login

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