首页 > 代码库 > QFile可以把自己的Parent设置成QHttpMultiPart,今天真是开了眼界了~
QFile可以把自己的Parent设置成QHttpMultiPart,今天真是开了眼界了~
SetParent函数是QObject,并不仅仅局限于GUI对象,这点和Delphi不一样。如此一来,以后可以给QNetworkAccessManager,QCoreApplication等等,都是如此,更不用说QTcpSocket,QTcpServer 。
QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType); QHttpPart imagePart; //imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("text/plain")); imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"file\"; filename=\"version.txt\""));/* version.tkt is the name on my Disk of the file that I want to upload */ QHttpPart textPart; textPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"name\"")); textPart.setBody("toto");/* toto is the name I give to my file in the server */ QString apkLocation = apktextEdit->text(); QFile *file = new QFile(apkLocation); file->open(QIODevice::ReadOnly); imagePart.setBodyDevice(file); file->setParent(multiPart); // we cannot delete the file now, so delete it with the multiPart multiPart->append(textPart); multiPart->append(imagePart); QUrl url("http://MyUrl.com"); QNetworkRequest request(url); QNetworkAccessManager *networkManager= new QNetworkAccessManager; reply = networkManager->post(request, multiPart); multiPart->setParent(reply); // delete the multiPart with the reply connect(reply, SIGNAL(finished()), this, SLOT (uploadDone())); connect(reply, SIGNAL(uploadProgress(qint64, qint64)), this, SLOT (uploadProgress(qint64, qint64)));}
https://stackoverflow.com/questions/38179706/uploading-a-file-in-multipart-form-data-in-qt5
QFile可以把自己的Parent设置成QHttpMultiPart,今天真是开了眼界了~
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。