QT HTTP upload form data format

SugarKmax 2022-01-26 20:22:57 阅读数:858

qt http upload form data

        QT Upload form format to cloud platform , utilize QMap binding KEY Values and VALUE value , Here is an example of the code :

QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
QMap<QString, QString>::iterator it1;
QMap<QString, QString>::iterator it2;
QMap<QString, QString> param1;
param1.insert("imgSize","1");
param1.insert("hd_device_id","2c9082ba7d8eccfe017d8edc56f90002");
param1.insert("time","2021-12-16 10:40:30");
QMap<QString, QString> param3;
param3.insert("img0","G:/Plant_Factory_picture/A1-A2/Snapshot-2021,11,30-12;02;52.jpg");
QVector<QFile*> qFiles;
for(it2 = param3.begin(); it2 != param3.end(); ++it2){
QHttpPart filePart;
// Cannot use directly when there are multiple files QFile file(path),for When the code block ends, it will be destructed , The data cannot be sent , Program crash
// A single file can not be used for Traverse , direct QFile file(path) The interface will be destructed at the end of this interface
QFile *file = new QFile(it2.value());
file->open(QFile::ReadOnly);
qFiles.push_back(file);
filePart.setBodyDevice(file);
filePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/jpeg"));// Here we upload pictures
filePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"" + it2.key() + "\"; filename=\""+file->fileName()+"\""));
multiPart->append(filePart);
}
for(it1 = param1.begin(); it1 != param1.end(); ++it1){
QHttpPart dataPart;
dataPart.setHeader(QNetworkRequest::ContentTypeHeader,QVariant("text/plain"));
dataPart.setHeader(QNetworkRequest::ContentDispositionHeader,QVariant("form-data; name=\"" + it1.key() + "\""));
dataPart.setBody(it1.value().toUtf8());
multiPart->append(dataPart);
}
//
QNetworkRequest request3;
QNetworkAccessManager* naManager3 = new QNetworkAccessManager(this);
QMetaObject::Connection connRet2 = QObject::connect(naManager3, SIGNAL(finished(QNetworkReply*)), this, SLOT(requestFinished(QNetworkReply*)));
request3.setUrl(QUrl("http://iot.e-jiankun.com//nyy/admin/hd/hd_device/deviceImgUpload"));
QString token_headerData = MainWindow:: token;
request3.setRawHeader("Authorization",token_headerData.toLocal8Bit());
qDebug()<<"token:"<<token_headerData<<endl;
naManager3->post(request3, multiPart);

copyright:author[SugarKmax],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/01/202201262022554215.html