以下是http post请求的内容 参数分别为 " filename1=111 , filename2=222 file=xxx.dll " 都是以 form-data 的方式提交
QByteArray TcHttp::post_file(const QString&curl,const QString &filepath,QString deviceid,QString filepath2,QString filepath_name,QString deviceid_name,QString seed_id
,QString filepath_name2,const int num)
{
QEventLoop loop;
QNetworkAccessManager manager;
QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
QHttpPart imagePart,textPart,textPart1,imagePart1;
textPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant(QString("form-data; name=\"%1\"").arg(deviceid_name).toStdString().data()));
textPart.setBody(deviceid.toUtf8());
textPart1.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"seed_1\""));
textPart1.setBody(seed_id.toUtf8());
imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/jpeg"));
imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant(QString("form-data; name=\"%1\";filename=\"%2\"").arg(filepath_name).arg(filepath)));
QFile *file = new QFile(filepath);
if(!file->open(QIODevice::ReadOnly))
{
return "1";
}
imagePart.setBodyDevice(file);
// file->setParent(multiPart); // we cannot delete the file now, so delete it with the multiPart
if(num==2)
{
imagePart1.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/jpeg"));
imagePart1.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant(QString("form-data; name=\"%1\";filename=\"%2\"")
.arg(filepath_name2).arg(filepath2)));
QFile *file = new QFile(filepath2);
if(!file->open(QIODevice::ReadOnly))
{
return "2";
}
imagePart1.setBodyDevice(file);
// file->setParent(multiPart); // we cannot delete the file now, so delete it with the multiPart
multiPart->append(imagePart1);
}
multiPart->append(textPart1);
multiPart->append(textPart);
multiPart->append(imagePart);
QUrl url(curl);
QNetworkRequest request(url);
QNetworkReply* reply = manager.post(request, multiPart);
QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()), Qt::DirectConnection);
QTimer::singleShot(m_timeout, &loop, SLOT(quit()));
loop.exec();
QByteArray temp=reply->readAll();
delete multiPart;
delete reply;
return temp;
}