Qt之QNetworkAccessManager 从本地和内存中上传数据到Http服务器

2023-12-16 21:56:06

简述

? ? ? ? 接连做了好几个服务器的项目,例如文件传输用的Ftp和对象存储服务器(Object Storage Service),简单的信息传输用的WebServer,之前也有用过HttpServer不过都和WebServer一样简单的调用接口提交数据并没有上传过文件,正好趁这次案例有机会做了个测试实例;

效果

从本地上传

? ? ? ? 从本地中读取一个图片上传,图片的ContentTypeHeader头部信息要填写和图片格式一致的类型,例如:jpg对应"image/jpg",bmp对应"image/bmp",png对应"image/png";ContentDispositionHeader头部信息的name要对应"file";QFile需要动态创建,因为要上传的文件可能很大,如果是局部的话函数退出就会销毁内存,导致上传失误甚至程序奔溃;最后因为QFile是动态创建,所以需要给其设置一个父类QHttpMultiPart,当父类被删除时也删除QFile;

void MainWindow::on_pbFromLocal_clicked()
{
    obtainData();

    // 文件路径
    QFile *file = new QFile("C:/Users/ilson/Desktop/HttpFileUp/logo.jpg");
    if (!file->open(QFile::ReadOnly)) {
        statusBar()->showMessage("file open failed");
        return;
    }

    // 设置请求
    QHttpPart imagePart;
    imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/jpg"));
    imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"file\";filename=\"test.jpg\""));
    imagePart.setBodyDevice(file);

    QHttpPart codePart;
    codePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"productCode\""));
    codePart.setBody("code");

    QHttpPart addressPart;
    addressPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"macAddress\""));
    addressPart.setBody("A1:B2:C3:D4:E5:G6");

    //
    QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
    multiPart->append(imagePart);
    multiPart->append(codePart);
    multiPart->append(addressPart);
    file->setParent(multiPart);  // 用 multiPart 删除 file

    QNetworkReply *reply = m_http.post(QNetworkRequest(makeUrl()), multiPart);
    multiPart->setParent(reply); // 用 reply 删除 multiPart
    connect(reply, SIGNAL(finished()), SLOT(replyFinished()));
}

从内存上传

? ? ? ? 从内存上传和从本地的区别就是数据需要重新保存成QIODevice类型;

// 压缩图像
// image     要压缩的图像
QByteArray MainWindow::compim(const QImage &image)
{
    QBuffer buffer;
    buffer.open(QIODevice::WriteOnly);
    image.save(&buffer, "jpg", 90);
    buffer.close();

    return buffer.data();
}

void MainWindow::on_pbFromMemory_clicked()
{
    obtainData();

    // 文件路径
    QImage image("C:/Users/ilson/Desktop/111.png");

    // 设置请求
    QHttpPart imagePart;
    imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/png"));
    imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"file\";filename=\"test.png\""));
    imagePart.setBody(compim(image));

    QHttpPart codePart;
    codePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"productCode\""));
    codePart.setBody("code");

    QHttpPart addressPart;
    addressPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"macAddress\""));
    addressPart.setBody("A1:B2:C3:D4:E5:G6");

    //
    QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
    multiPart->append(imagePart);
    multiPart->append(codePart);
    multiPart->append(addressPart);

    QNetworkReply *reply = m_http.post(QNetworkRequest(makeUrl()), multiPart);
    multiPart->setParent(reply); // 用 reply 删除 multiPart
    connect(reply, SIGNAL(finished()), SLOT(replyFinished()));
}

注意?

????????QHttpMultiPart的删除需要给其设置父类QNetworkReply,当QNetworkReply被删除时其子类也自会删除;

其他

? ? ? ? 源码下载(折扣):https://item.taobao.com/item.htm?ft=t&id=756129976580

文章来源:https://blog.csdn.net/Ilson_/article/details/135026684
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。