我制作了一个应用程序,可以从Open API下载数据并将其编译为格式化的.XLSX文件。然后,我需要将此文件上传到使用用户名和密码的本地FTP服务器上。我尝试使用一个开放源代码示例将其上传到FTP,它可以完美运行。但是,将其转移到我自己的应用程序中时,它将失败。如何成功通过FTP传输数据?我下面有我的代码段,因为它们实际上是相同的。
示例Cpp:
#include "widget.h"
#include "ui_widget.h"
#include <QFileDialog>
#include <QUrl>
#include <QDebug>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
m_manager = new QNetworkAccessManager(this);
connect(m_manager, &QNetworkAccessManager::finished, this, &Widget::uploadFinished);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_selectFileButton_clicked()
{
m_fileName = QFileDialog::getOpenFileName(this, "Get Any File");
ui->fileNameLineEdit->setText(m_fileName);
}
void Widget::on_uploadButton_clicked()
{
m_file = new QFile(m_fileName);
QFileInfo fileInfo(*m_file);
QUrl url(ui->uploadUrlLineEdit->text() + fileInfo.fileName());
url.setUserName("BoardmanOH");
url.setPassword("_4WMAe,^Mo=jYt6mYw@iz7Ri");
url.setPort(21);
if (m_file->open(QIODevice::ReadOnly))
{
QNetworkReply *reply = m_manager->put(QNetworkRequest(url), m_file);
connect(reply, &QNetworkReply::uploadProgress, this, &Widget::uploadProgress);
}
}
void Widget::uploadFinished(QNetworkReply *reply)
{
if (!reply->error())
{
m_file->close();
m_file->deleteLater();
reply->deleteLater();
}
}
void Widget::uploadProgress(qint64 bytesSent, qint64 bytesTotal)
{
ui->progressBar->setValue(100 * bytesSent/bytesTotal);
}
示例头文件:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QString>
#include <QFile>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private slots:
void on_selectFileButton_clicked();
void on_uploadButton_clicked();
void uploadFinished(QNetworkReply *reply);
void uploadProgress(qint64 bytesSent, qint64 bytesTotal);
private:
Ui::Widget *ui;
QNetworkAccessManager *m_manager;
QString m_fileName;
QFile *m_file;
};
#endif // WIDGET_H
我的CPP文件:
#include "ftpuploader.h"
FTPUploader::FTPUploader(QString filename)
{
m_fileName = filename;
m_manager = new QNetworkAccessManager(this);
connect(m_manager, &QNetworkAccessManager::finished, this, &FTPUploader::uploadFinished);
}
void FTPUploader::start()
{
m_file = new QFile(m_fileName);
QFileInfo fileInfo(*m_file);
QUrl url("ftp://********.com/" + fileInfo.fileName());
url.setUserName("*******");
url.setPassword("*********");
url.setPort(21);
if (m_file->open(QIODevice::ReadOnly))
{
QNetworkReply *reply = m_manager->put(QNetworkRequest(url), m_file);
connect(reply, &QNetworkReply::uploadProgress, this, &FTPUploader::uploadProgress);
}
}
void FTPUploader::uploadProgress(qint64 bytesSent, qint64 bytesTotal)
{
qDebug() << "Uploaded: " << bytesSent << "of" << bytesTotal;
//ui->progressBar->setValue(100 * bytesSent/bytesTotal);
}
void FTPUploader::uploadFinished(QNetworkReply *reply)
{
if (!reply->error())
{
m_file->close();
m_file->deleteLater();
reply->deleteLater();
}
}
我的头文件:
#ifndef FTPUPLOADER
#define FTPUPLOADER
#include <QtCore>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QDebug>
class FTPUploader: public QObject
{
Q_OBJECT
public:
explicit FTPUploader(QString filename = nullptr);
void start();
private slots:
void uploadFinished(QNetworkReply *reply);
void uploadProgress(qint64 bytesSent, qint64 bytesTotal);
private:
QNetworkAccessManager *m_manager;
QString m_fileName;
QFile *m_file;
};
#endif
我发现这些文件实际上在功能上是相同的,但是它们根本无法按预期运行。感谢您提供的任何帮助或评论!