我正尝试在CentOS 6.4上的Qt 5.0中使用QNetworkAccessManager将文件上传到服务器。
我尝试过在线跟踪几个例子,但没有一个可行。 QFTP工作正常,但很慢,现在已弃用。我上传的代码是:
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();
}
这是我程序的输出:
Completed: 0/0 0%
Finished
"Cannot open ftp://username:password@example.com/: is a directory"
Error code: 202
查看文档,202表示:
QNetworkReply::ContentOperationNotPermittedError The operation requested on the remote content is not permitted
有什么建议吗?
答案 0 :(得分:4)
变化:
QUrl url2("ftp://example.com");
到
QUrl url2("ftp://example.com/somefile");
有必要指出文件的链接。