问题
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:password@example.com/: 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?
回答1:
Change:
QUrl url2("ftp://example.com");
to
QUrl url2("ftp://example.com/somefile");
It is necessary to point out the link to a file.
来源:https://stackoverflow.com/questions/16877363/file-upload-error-with-qnetworkaccessmanager