码迷,mamicode.com
首页 > Web开发 > 详细

QFile可以把自己的Parent设置成QHttpMultiPart,今天真是开了眼界了~

时间:2017-07-01 10:05:02      阅读:1375      评论:0      收藏:0      [点我收藏+]

标签:oca   ati   new   let   tac   add   tip   edit   sig   

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,今天真是开了眼界了~

标签:oca   ati   new   let   tac   add   tip   edit   sig   

原文地址:http://www.cnblogs.com/findumars/p/7101219.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!