QT QNetworkAccessManager to download file from ftp server not working
c++, ftp, qnetworkaccessmanager, qnetworkrequest, qt
Solution
get() doesn't perform the GET request right away synchronously, but just creates a QNetworkReply object, where the actual request will be performed asynchronously at a later point.
`readAll()` reads only the data available at a given time but doesn't block to wait for more data. Right after creation, there isn't any data available.
To wait for all data to be downloaded, connect to the finished() and error() signals:
connect(reply, SIGNAL(finished()), this, SLOT(requestFinished()));
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), SLOT(requestError(QNetworkReply::NetworkError));
In the `requestFinished()` slot you can then use `readAll()`. That works ok when downloaing small files only. When downloading larger files, better connect to the readyRead() signal and handle the arriving data in incremental chunks instead of using a single `readAll()` in the very end.
Problem
``` QNetworkAccessManager *nam = new QNetworkAccessManager(); QUrl url2("ftp://127.0.0.1/test.txt/"); url2.setPassword("12345"); url2.setUserName("user"); QNetworkRequest req(url2); QNetworkReply *reply = nam->get(req); QByteArray data = reply->readAll() ; qDebug() << data ; ``` It connects to the local ftp server and reads the file but it gets garbage what am I doing wrong??