当前位置:网站首页>Summary of FTP function implemented by qnetworkaccessmanager
Summary of FTP function implemented by qnetworkaccessmanager
2022-07-06 16:17:00 【Larry_ Yanan】
Because the project needs , First contact FTP Upload problem of , After consulting, the old QFtp, Adopt the new QNetworkAccessManager. Its use is also very convenient , Relatively mature interfaces and signals have been provided , So to summarize .
First , Create some variables and pointers in the header file
QUrl url;
QNetworkAccessManager *accessManager; // Initialize to nullptr
QNetworkReply *reply; // Initialize to nullptr
QFile *ftp_file; // Initialize to nullptr
bool ftp_upload = false; // Upload file ID
bool ftp_download = false; // Upload file ID
url It needs to be uploaded later FTP Server file path ;QNetworkAccessManager It is the so-called network access manager , It is mainly used to upload files ;QNetworkReply yes QNetworkAccessManager A reply object when uploading , You can get some returned information through it
then , yes cpp The formal code part of the document
//url
QUrl url(uploadUrl);// Set up a ftp route , for example ftp://xxx/xx/x
url.setPort(ftp_upload_path_info.ftpport);// Port number
url.setUserName(ftp_upload_path_info.ftpaccount);// user name
url.setPassword(ftp_upload_path_info.ftppws);// password
// Add an instance to the pointer
accessManager = new QNetworkAccessManager();
accessManager->setNetworkAccessible(QNetworkAccessManager::Accessible);// Set up network access
QNetworkRequest request(url);// take QUrl Set into the network request class QNetworkRequest in
// The method of reading the file , stay 1G The above files will cause direct upload failure
//ftp_file = new QFile(filenamepath);
//ftp_file->open(QIODevice::ReadOnly);
//QByteArray byte_file = ftp_file->readAll();
// Solve the uploading of large files
QFile *data = new QFile(filenamepath,this);
if(data->open(QIODevice::ReadOnly))
{
}
reply = accessManager->put(request, data/*byte_file*/); // Send upload request
connect(accessManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(loadError(QNetworkReply::NetworkError)));
connect(reply, SIGNAL(uploadProgress(qint64 ,qint64)), this, SLOT(loadProgress(qint64 ,qint64)));
Not a lot of code , Step by step . First QUrl You need to set the path 、 Port number 、 User name and password , This is the FTP Server related ; Then there is the formal QNetworkAccessManager Upload , You need to put url Set in QNetworkRequest In the object , Then pass it in the form of parameters QNetworkAccessManager Of put In the method .put Actually, it means uploading , except QNetworkRequest Out of parameters , You also need a file pointer .
There are two ways , The mainstream online is to use QFile Of readAll() To read , But there is no way for large files readAll() Of , So it will fail directly . Here will be passed in QByteArray data , Change to direct transfer QIODevice Type a pointer (QFile Inherit ), So as to solve the problem of uploading large files . Personal test 2G That's OK , It's just that the larger the file, the slower the upload speed .
Reference resources : solve QNetworkAccessManager Realization ftp After function , Cannot upload more than 1G Problems with documents
The other is signal slot Correlation , Here I bound three . The first is QNetworkAccessManager Of itself finished The signal , Whether the upload is successful , Or upload failed , Network interruption timeout and other things will be returned , It's a result anyway , You need to go through QNetworkReply Parameter to distinguish the returned results .
void xxx::replyFinished(QNetworkReply *)
{
qDebug()<<"reply->error()"<<reply->error();
if (reply->error() == QNetworkReply::NoError) // After each file is uploaded, it will be executed in Chengdu
{
qDebug()<<" Upload successful ";
ftp_file->close();// Remember to close the file operation
QMessageBox::about(NULL, " Tips ", " Upload successful !");
}else if((reply->error() == QNetworkReply::OperationCanceledError))
{
// Error caused by manual cancellation
}
else
{
// QMessageBox::about(NULL, " Tips ", " Upload failed !");
}
}
The second signal slot is also an error message , Similar to the first one , But the timing of reporting errors may be different
void xxx::loadError(QNetworkReply::NetworkError error)
{
qDebug()<<"Transmitted error!!!";
//QMessageBox::about(NULL, " Tips ", " Upload failed !");
}
The third signal slot is a little interesting , It is a feedback signal of the upload progress ,uploadProgress(qint64 ,qint64) The parameters of are the current uploaded size and total size , for example 250,1000 such .
The trigger frequency of this signal is moderate , You can use it to display a progress bar . For example QProgressBar Of void setRange(int minimum, int maximum); and void setValue(int value); To set the range and current value .
It is worth mentioning that , The parameters here are int type , In the case of large file uploading , Often uploadProgress(qint64 ,qint64) Of qint64 (long long) It is greater than int Of , If you don't judge and convert by yourself , The progress bar will have no effect or percentage value .
while(bytesTotal > 0x7fffffff)//int The maximum of
{
bytesTotal /= 10;
bytesSent /= 10;
}
边栏推荐
- Sanic异步框架真的这么强吗?实践中找真理
- Radar equipment (greedy)
- 树莓派4B64位系统安装miniconda(折腾了几天终于解决)
- QT有关QCobobox控件的样式设置(圆角、下拉框,向上展开、可编辑、内部布局等)
- Codeforces Round #797 (Div. 3)无F
- Luogu P1102 A-B number pair (dichotomy, map, double pointer)
- 875. 爱吃香蕉的珂珂 - 力扣(LeetCode)
- 1903. Maximum odd number in string
- 7-1 understand everything (20 points)
- socket通讯
猜你喜欢

Differential (one-dimensional, two-dimensional, three-dimensional) Blue Bridge Cup three body attack

VMware Tools和open-vm-tools的安装与使用:解决虚拟机不全屏和无法传输文件的问题

Codeforces Round #799 (Div. 4)A~H

Maximum product (greedy)

Ball Dropping

1529. Minimum number of suffix flips

969. Pancake sorting

1605. Sum the feasible matrix for a given row and column

(POJ - 3579) median (two points)

Codeforces Round #797 (Div. 3)无F
随机推荐
Opencv learning log 26 -- detect circular holes and mark them
HDU - 6024 building shops (girls' competition)
Pytorch extract skeleton (differentiable)
树莓派4B64位系统安装miniconda(折腾了几天终于解决)
Codeforces Round #800 (Div. 2)AC
树莓派4B安装opencv3.4.0
Hdu-6025-prime sequence (girls' competition)
Opencv learning log 27 -- chip positioning
Problem - 1646C. Factorials and Powers of Two - Codeforces
Openwrt build Hello ipk
力扣——第298场周赛
(POJ - 3258) River hopper (two points)
Educational Codeforces Round 130 (Rated for Div. 2)A~C
[exercise -11] 4 values why sum is 0 (and 4 values of 0)
QT模拟鼠标事件,实现点击双击移动拖拽等
Generate random password / verification code
Understand what is a programming language in a popular way
Li Kou - 298th weekly match
C language is the watershed between low-level and high-level
MariaDB的安装与配置