qt http使用post表单上传

1 篇文章 0 订阅

 

以下是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;
}

​

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值