我想用QFtp将文本文件上传到FTP服务器。如何使用QFtp :: put上传Qt C++中的文件?
这是我的代码:此代码执行
QFile *file = new QFile("test.txt");
QFtp *ftp = new QFtp();
if(file->open(QIODevice::ReadWrite)) {
ftp->setTransferMode(QFtp::Active);
ftp->connectToHost(server);
ftp->login(name, password);
ftp->put(file, "test.txt");
ftp->close();
}
后,我没有看到我的FTP服务器上的任何东西。当我查看QFtp :: put的文档时,我发现第一个参数应该是QIODevice或QByteArray。我应该怎么做?
编辑:
所以我现在有这样的代码:
//ftp.cpp
QFile *file = new QFile("test.txt");
QFtp *ftp = new QFtp();
this->connect(ftp, SIGNAL(commandStarted(int)), SLOT(ftpCommandStarted(int)));
this->connect(ftp, SIGNAL(commandFinished(int, bool)), SLOT(ftpCommandFinished(int, bool)));
this->connect(ftp, SIGNAL(done(bool)), SLOT(ftpDone(bool)));
this->connect(ftp, SIGNAL(dataTransferProgress(qint64, qint64)), SLOT(ftpDataTransferProgress(qint64, qint64)));
this->connect(ftp, SIGNAL(stateChanged(int)), SLOT(ftpStateChanged(int)));
if(file->open(QIODevice::ReadWrite)) {
ftp->setTransferMode(QFtp::Active);
ftp->connectToHost(server);
ftp->login(name, password);
ftp->put(file, "test.txt");
ftp->close();
}
具有以下功能:
//ftp.h
void ftpCommandStarted(int id);
void ftpCommandFinished(int id, bool error);
void ftpDone(bool);
void ftpDataTransferProgress(qint64, qint64);
void ftpStateChanged(int);
//ftp.cpp
void EmailDialog::ftpCommandStarted(int id) {
this->messageBox("Command Started: " + QString::number(id));
}
void EmailDialog::ftpCommandFinished(int id, bool error) {
this->messageBox("Command Finished: " + QString::number(id) + " Error: " + (error ? "Error" : "No Error"));
}
void EmailDialog::ftpDone(bool error) {
this->messageBox("Done " + QString(error ? "Error" : "No Error"));
}
void EmailDialog::ftpDataTransferProgress(qint64 done, qint64 total) {
this->messageBox("Done: " + QString::number(done) + " Total: " + QString::number(total));
}
void EmailDialog::ftpStateChanged(int state) {
QString text;
switch (state) {
case 0:
text = "QFtp::Unconnected";
break;
case 1:
text = "QFtp::HostLookup";
break;
case 2:
text = "QFtp::Connecting";
break;
case 3:
text = "QFtp::Connected";
break;
case 4:
text = "QFtp::LoggingIn";
break;
case 5:
text = "QFtp::Closing";
break;
default:
text = "";
break;
}
this->messageBox(text);
}
但是,我没有得到任何迹象表明该插槽被调用。我没有任何消息框弹出。我在这里做错了什么?
现在好多了,但是我错过了太多的代码来说出了什么问题。你能上传到github或bitbucket吗? – 2011-12-19 19:34:32
这已经有一段时间了,你可以考虑选择一个答案...... – 2014-08-07 23:45:03
@RickSmith这些都没有工作。我最终放弃了这一点,并做了其他事情。 – 2014-08-08 13:31:03