I am trying to upload a file to a server using QNetworkAccessManager in Qt 5.0 on CentOS 6.4.
I have tried following a few examples online but none of them work. QFTP works just fine but is slow and now deprecated. My code for the upload is:
void ftp::start(QString fileLocation)
{
QUrl url2("ftp://example.com");
url2.setUserName(ftpusername);
url2.setPassword(ftppassword);
data = new QFile(fileLocation, this);
if (data->open(QIODevice::ReadOnly)) {
nam = new QNetworkAccessManager();
reply = nam->put(QNetworkRequest(url2), data);
connect(nam, SIGNAL(finished(QNetworkReply*)),this, SLOT(requestFinished(QNetworkReply*)));
connect(reply, SIGNAL(uploadProgress(qint64, qint64)), SLOT(uploadProgress2(qint64, qint64)));
connect(reply, SIGNAL(finished()), SLOT(uploadDone()));
}
else
{
qDebug() << "Could not open file to FTP";
}
}
void ftp::uploadProgress2(qint64 done, qint64 total) {
double percent;
if(done > 0 && total > 0)
{
percent = (done*100)/total;
}
myParent->addLog("Completed: " + QString::number(done) + "/" + QString::number(total) + " " + QString::number(percent) + "%");
}
void ftp::uploadDone() {
qDebug() << "Error Code: " << reply->error();
data->deleteLater();
reply->deleteLater();
}
void ftp::requestFinished(QNetworkReply* r)
{
qDebug() << "Finished ";
qDebug()<< r->errorString();
}
This is the output from my program:
Completed: 0/0 0%
Finished
"Cannot open ftp://username:[email protected]/: is a directory"
Error code: 202
Looking at the docs, 202 means:
QNetworkReply::ContentOperationNotPermittedError The operation requested on the remote content is not permitted
Any suggestions?
Change:
QUrl url2("ftp://example.com");
to
QUrl url2("ftp://example.com/somefile");
It is necessary to point out the link to a file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With