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
时间: 2024-10-10 10:38:50