发送一个文件到指定的服务器地址
QNetworkAccessManager *manager = new QNetworkAccessManager(this);
QUrl url("http://172.16.21.47:8889/financeContract/financeContractRecordDataImport"); // 替换为你的上传URL
QNetworkRequest request(url);
QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
// 添加其他表单字段(如果需要)
// QHttpPart part;
// part.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"fieldName\""));
// part.setBody("fieldValue");
// multiPart->append(part);
// 添加文件
QHttpPart filePart;
//上传固定的名称
//filePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"file\"; filename=\"myfile.txt\""));
//使用上传源文件名称
QFileInfo fileInfo(fileName);
QString docName = fileInfo.fileName();
filePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"file\"; filename=\"" + docName + "\""));
QFile *file = new QFile(fileName); // 替换为你的文件路径
if (file->open(QIODevice::ReadOnly)) {
filePart.setBodyDevice(file);
//file->close();
}
multiPart->append(filePart);
QNetworkReply *reply = manager->post(request, multiPart);
multiPart->setParent(reply); // 设置parent以确保在回复完成后删除multiPart
// 处理响应...
connect(reply, &QNetworkReply::finished, this, [reply]() {
if (reply->error() == QNetworkReply::NoError) {
// 处理成功响应
QString response = reply->readAll();
// ...
}
else {
// 处理错误
// ...
}
reply->deleteLater();
});
检查本地程序版本号
m_http = new QNetworkAccessManager();
//设置头信息
QNetworkRequest m_url;
m_url.setUrl(QUrl("https://pmp.eloam.net/api/ota/findFadVersion"));
m_url.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");//区别于上传文件
QSslConfiguration m_sslConfig = QSslConfiguration::defaultConfiguration();
m_sslConfig.setPeerVerifyMode(QSslSocket::VerifyNone);
m_sslConfig.setProtocol(QSsl::TlsV1_2);
m_url.setSslConfiguration(m_sslConfig);
QJsonObject object;
object.insert("as", "758686D6");
object.insert("ks", "9f0c219ca61e41b7b3e2b1f325640c9a");
object.insert("productCode", "HSPS");
object.insert("version", "V7.1.3");
QByteArray byte_array = QJsonDocument(object).toJson();
m_res = m_http->post(m_url, byte_array);//发起post请求
connect(m_res, &QNetworkReply::finished, this, &Update::requestFinished);
标签:QNetworkRequest,QHttpMultiPart,版本号,object,url,multiPart,file,reply,Qt
From: https://www.cnblogs.com/gblzacy/p/18183666