Qt的FTP上传下载相关代码:
//初始化ftp ftp = new QFtp(this); ftp->connectToHost(SERVERIP); ftp->login(USERNAME, PASSWORD); //命令开始执行时 connect(ftp, SIGNAL(commandStarted(int)), this, SLOT(ftpCommandStarted(int))); //当每条命令开始执行时发出相应的信号 connect(ftp, SIGNAL(commandFinished(int, bool)), this, SLOT(ftpCommandFinished(int, bool)));
对应的处理方法:
//命令开始执行时
void mainwidget::ftpCommandStarted(int) {
if(ftp->currentCommand() == QFtp::ConnectToHost){
//QMessageBox::critical(0, "ftp连接状态", "正在连接到服务器...", QMessageBox::Cancel);
qDebug() << "正在连接到服务器...";
}
if (ftp->currentCommand() == QFtp::Login){
//ui.label->setText(tr("正在登录…"));
qDebug() << "正在登录...";
}
if (ftp->currentCommand() == QFtp::Get){
//ui.label->setText(tr("正在下载…"));
qDebug() << "正在下载...";
} else if (ftp->currentCommand() == QFtp::Close){
//ui.label->setText(tr("正在关闭连接…"));
qDebug() << "正在关闭连接...";
}
}
//命令执行
void mainwidget::ftpCommandFinished(int, bool error) {
if (ftp->currentCommand() == QFtp::ConnectToHost) {
if (error) {
//ui.label->setText(tr("连接服务器出现错误:%1").arg(ftp->errorString() ));
qDebug() << tr("连接服务器出现错误:%1").arg(ftp->errorString());
//QMessageBox::critical(0, "FTP连接错误", tr("连接服务器出现错误:%1").arg(ftp->errorString()), QMessageBox::Cancel);
} else {
//ui.label->setText(tr("连接到服务器成功"));
qDebug() << tr("连接到服务器成功");
}
}
if (ftp->currentCommand() == QFtp::Login) {
if (error) {
//ui.label->setText(tr("登录出现错误:%1").arg(ftp->errorString()));
qDebug() << tr("登录出现错误:%1").arg(ftp->errorString());
//QMessageBox::critical(0, "FTP连接错误", tr("登录出现错误:%1").arg(ftp->errorString()), QMessageBox::Cancel);
} else {
//ui.label->setText(tr("登录成功"));
qDebug() << tr("登录成功");
}
}
if (ftp->currentCommand() == QFtp::Put) {
if (error) {
//ui.label->setText(tr("上传出现错误:%1").arg(ftp->errorString()));
//ui.textEdit->setText(tr("上传出现错误:%1").arg(ftp->errorString()));
qDebug() << tr("上传出现错误:%1").arg(ftp->errorString());
QMessageBox::critical(0, "FTP连接错误", tr("上传出现错误:%1").arg(ftp->errorString()), QMessageBox::Cancel);
} else {
//ui.label->setText(tr("已经完成上传"));
qDebug() << tr("已经完成上传");
}
}
if (ftp->currentCommand() == QFtp::Get) {
if(error) {
//ui.textEdit->setText(tr("下载出现错误:%1").arg(ftp->errorString()));
qDebug() << tr("下载出现错误:%1").arg(ftp->errorString());
QMessageBox::critical(0, "FTP连接错误", tr("下载出现错误:%1").arg(ftp->errorString()), QMessageBox::Cancel);
} else {
//ui.label->setText(tr("已经完成下载"));
qDebug() << tr("已经完成下载");
}
} else if (ftp->currentCommand() == QFtp::Close) {
//ui.label->setText(tr("已经关闭连接"));
qDebug() << tr("已经关闭连接");
}
}
具体调用:
QString path = QCoreApplication::applicationDirPath();
QFile *uploadFile = new QFile(path + "/addr.xml");
QString des = "addr.xml";
uploadFile->open(QIODevice::ReadWrite);
int icd = ftp->cd("/index/sjpc/");
int iput = ftp->put(uploadFile, des);
qDebug() << "upload finished!\n" << icd << ", " << iput;
HTTP上传:
参考:
http://blog.163.com/charlie-lei@126/blog/static/11374392420108297578992/
http://blog.csdn.net/songjinshi/article/details/11806773
核心代码:
//上传
QNetworkAccessManager *_uploadManager;
QNetworkReply *_reply;
char* m_buf;
...
_uploadManager = new QNetworkAccessManager(this);
connect(_uploadManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyUploadFinished(QNetworkReply*)));
m_buf = NULL;
...
/*http上传*/
QString path = QCoreApplication::applicationDirPath();
QFile file(path + "/addr.xml");
if (!file.open(QIODevice::ReadOnly)) {
QMessageBox::critical(0, "Cannot Upload Configure File", "Please check the configure file!", QMessageBox::Cancel);
} else {
int file_len = file.size();
QDataStream in(&file);
m_buf = new char[file_len];
in.readRawData(m_buf, file_len);
file.close();
QNetworkRequest request(QUrl("http://www.sensikeji.com/sjpc/upload.php?filename=addr.xml"));
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/octet-stream");
QByteArray arr = QByteArray(m_buf, file_len);
_reply = _uploadManager->post(request , arr);
connect(_reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(upLoadError(QNetworkReply::NetworkError)));
}
文章的脚注信息由WordPress的wp-posturl插件自动生成
微信扫一扫,打赏作者吧~![[整理]how to run flask with pyqt5](http://www.jyguagua.com/wp-content/themes/begin/timthumb.php?src=http://www.jyguagua.com/wp-content/uploads/2021/03/pyqt_flask.png&w=280&h=210&zc=1)


