跳转到帖子
墨香年少

Qt下上传文件和参数到web api

已推荐帖子

void Uploador::on_submit_photo_clicked()
{
    QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);

    QHttpPart _uid;
    _uid.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"uid\""));
    _uid.setBody(uid.toUtf8());
    multiPart->append(_uid);

    QHttpPart _key;
    _key.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"key\""));
    _key.setBody("ABCD");
    multiPart->append(_key);

    QHttpPart tit;
    tit.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"title\""));
    tit.setBody(ui->lineEdit_title->text().toUtf8());
    multiPart->append(tit);

    QHttpPart tags;
    tags.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"tags\""));
    tags.setBody(ui->lineEdit_tag->text().toUtf8());
    multiPart->append(tags);

    QHttpPart _filename;
    _filename.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"filename\""));
    _filename.setBody(filename.toUtf8());
    multiPart->append(_filename);

    QString ext = get_ext(filename);

    QHttpPart imagePart;
    imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant(ext));
    imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"stream\""));

    file = new QFile();
    if(local.length() > 2)
    {
        file = new QFile(local);
        local_file = local;
    }
    else
    {
        local_file = tmp_folder+filename;
        file = new QFile(local_file);
    }

    file->open(QIODevice::ReadOnly);
    imagePart.setBodyDevice(file);
    file->setParent(multiPart);
    multiPart->append(imagePart);

    QUrl url("http://www.abc.com/client/api");
    QNetworkRequest request(url);

    QNetworkAccessManager *mgr = new QNetworkAccessManager();
    QNetworkReply *reply = mgr->post(request, multiPart);
    multiPart->setParent(reply);

    connect(mgr,SIGNAL(finished(QNetworkReply*)),this,SLOT(slot_requestFinished(QNetworkReply*)));
}

 

  • 有帮助 3

目之所及,皆是回忆,心之所想,皆是过往

分享这篇帖子


链接帖子
分享到其他站点

创建帐户或登录来提出意见

你需要成为会员才能提出意见

创建帐户

注册成为会员。只要几个简单步骤!

注册帐户

登录

已有帐户? 请登录。

现在登录

×
×
  • 创建新的...

重要信息

注册必须使用2-8个中文汉字作为账号