problem to download 2 files one after the other with QNetworkAccesManager
-
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
-
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
-
@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?
-
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 ; }}
-
@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"); }
-
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 ?
-
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 !