问题
I use a QNAM to handle uploads using a ftp protocol. The whole process works but I have a strange behavior:
this is my method :
void ftp::uploadFile(const QString &origin, const QString &destination)
{
QUrl url("ftp://"+host+""+destination);
url.setUserName(user);
url.setPassword(pwd);
url.setPort(21);
localFile = new QFile(origin, this);
if (localFile->open(QIODevice::ReadOnly))
{
reply = nam->put(QNetworkRequest(url), localFile);
QObject::connect(reply, SIGNAL(uploadProgress(qint64, qint64)), SLOT(transferProgress(qint64, qint64)));
QObject::connect(reply, SIGNAL(finished()), this, SLOT(transferFinished()));
}
else qDebug() << localFile->errorString();
}
When I upload a file, the uploadProgress is emitted :
qDebug() << sent << "/" << total;
outputs the 0/x till the x/x . Then it takes a long time, maybe up to 20 seconds before the finished signal is emitted. Why this delay?
I tried ignoring the finished signal and emit the signal myself when the progress is at sent==total
but the file is corrupted at the other end. (It's not really corrupted, as I only send jpg, The resulting file is an upper-half only jpg. a big part is just grey.)
I'd like to provide my users with a progress bar where 100% really means the process is finished. Uploading for 5 seconds, then staying for 20 seconds at 100% isn't really nice.
回答1:
file upload does some buffering in background (qt socket buffers, system socket buffers, network buffers) so 'progress' signal just means you send the data to somewhere nor that server has received it. While 'finished' signal is emitted when all data transferred to remote side and buffers are flushed. If you need to know exact size transferred you may look for disabling request or socket(s) or qnam buffring/caching.
来源:https://stackoverflow.com/questions/24363568/qt-qnetworkaccessmanager-long-delay-to-emit-finished-signal