墨香年少 32 发布于 2020年12月25日 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 目之所及,皆是回忆,心之所想,皆是过往 分享这篇帖子 链接帖子 分享到其他站点