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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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,今天真是开了眼界了~的主要内容,如果未能解决你的问题,请参考以下文章

如何阻止 QHttp/QtWebKit 缓存页面

QHttp 示例 qt 4.4.3 的问题

Qt笔记——QFile,QDataStream,QTextStream

怎样把自己的电脑设置成服务器?

Qt QFile文件读写

怎样把自己设置成管理员