• 2386阅读
  • 6回复

Qt HTTP上传文件/下载文件 [复制链接]

上一主题 下一主题
在线shixingya
 

只看楼主 倒序阅读 楼主  发表于: 2017-12-15

一  、 上传文件
QString  url_ ="http://172.24.25.34:8288/storage/uploadImage";
    QString uploadFilename="D:/12121.png";

    QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);

    QHttpPart textPart;
    textPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"watermark\""));
    textPart.setBody("my text");
    multiPart->append(textPart);

    QHttpPart imagePart;
    QFile *file = new QFile(uploadFilename);
    file->open(QIODevice::ReadOnly);
    imagePart.setBodyDevice(file);
    imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"filename\"; filename=\"12121.png\""));

    multiPart->append(imagePart);

    QUrl url(url_);
    QNetworkRequest request(url);

    QNetworkAccessManager *networkManager= new QNetworkAccessManager;
    QNetworkReply *_reply = networkManager->post(request, multiPart);
    multiPart->setParent(_reply); // delete the multiPart with the reply

    QEventLoop loop;
    connect(_reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(upLoadError(QNetworkReply::NetworkError)));
    connect(_reply, SIGNAL(uploadProgress ( qint64 ,qint64 )), this, SLOT( OnUploadProgress(qint64 ,qint64 )));
    connect(_reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);

    loop.exec();

    //获取http状态码
    int http_code_ = _reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
    // 获取数据
    if (QNetworkReply::NoError == _reply->error())
    {
        QByteArray context_ = _reply->readAll();
        QJsonObject result_obj = QJsonDocument::fromJson(context_).object();
        int code = result_obj.value("code").toInt();
        qDebug()<<"code === "<<code<<result_obj;
    }


二、 下载文件

void downloadFile(QString url, QString localPath)
{
    if(nullptr==file)
        file=new QFile();
    file->setFileName(localPath);
    //判断文件是否存在
    if(file->exists()){
        qDebug()<<"文件已存在";
    }else{
        qDebug()<<"文件不存在";
    }
    //已读写方式打开文件,
    //如果文件不存在会自动创建文件
    if(!file->open(QIODevice::ReadWrite)){
        qDebug()<<"打开失败";
    }else{
        qDebug()<<"打开成功";
    }
    if (!file)
        return;

    const QUrl newUrl = QUrl::fromUserInput(url);
    QNetworkAccessManager *accessManager = new QNetworkAccessManager(this);
    accessManager->setNetworkAccessible(QNetworkAccessManager::Accessible);

    reply= (*accessManager).get(QNetworkRequest(newUrl));
    connect(reply, SIGNAL(downloadProgress(qint64 ,qint64)), this, SIGNAL(updataProgress(qint64,qint64)),Qt::AutoConnection);
    connect(accessManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));

    connect(reply, &QIODevice::readyRead, this, &TaskRunner::readContent);
    _thread=new QThread;
    this->moveToThread(_thread);
    _thread->start();
}
自2012年接触QT以来,写QT一直是我每天的工作,这些年在中国运载火箭技术研究院、国美电器、启明星辰、广联达等公司工作过,为军工科研、政府项目、电商项目、在线教育用Qt开发过多个的工具或系统,也接触过中科方德Linux、Mac Qt、Windows Qt, 有同行的朋友,可以加我微信,一起进步
我叫史星亚 微信: EagleAndy  
加好友时请备注  来自qtcn
非工作时间 可以电联 18610042411
在线shixingya

只看该作者 1楼 发表于: 2017-12-15
上传多个文件:

QString  url_ ="http://172.24.25.34:8288/storage/uploadImage";
    QStringList fileListStr ;
    fileListStr<<"D:/33.jpeg"<<"D:/2232.png";

    QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
    for(int i=0; i<fileListStr.count(); i++)
    {
        QHttpPart imagePart;
        QString filePath =fileListStr;
        QFile *file = new QFile(filePath);
        file->open(QIODevice::ReadOnly);
        imagePart.setBodyDevice(file);
        QString filename =filePath.split("/").last();
        QString contentVariant=QString("form-data; name=\"filename\"; filename=\"%1\"").arg(filename);
        imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant(contentVariant));
        multiPart->append(imagePart);
    }



    QUrl url(url_);
    QNetworkRequest request(url);

    QNetworkAccessManager *networkManager= new QNetworkAccessManager;
    QNetworkReply *_reply = networkManager->post(request, multiPart);
    multiPart->setParent(_reply); // delete the multiPart with the reply

    QEventLoop loop;
    connect(_reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(upLoadError(QNetworkReply::NetworkError)));
    connect(_reply, SIGNAL(uploadProgress ( qint64 ,qint64 )), this, SLOT( OnUploadProgress(qint64 ,qint64 )));
    connect(_reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);

    loop.exec();

    //获取http状态码
    int http_code_ = _reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
    // 获取数据
    if (QNetworkReply::NoError == _reply->error())
    {
        QByteArray context_ = _reply->readAll();
        QJsonObject result_obj = QJsonDocument::fromJson(context_).object();
        int code = result_obj.value("code").toInt();
        qDebug()<<"code === "<<code<<result_obj;
    }
自2012年接触QT以来,写QT一直是我每天的工作,这些年在中国运载火箭技术研究院、国美电器、启明星辰、广联达等公司工作过,为军工科研、政府项目、电商项目、在线教育用Qt开发过多个的工具或系统,也接触过中科方德Linux、Mac Qt、Windows Qt, 有同行的朋友,可以加我微信,一起进步
我叫史星亚 微信: EagleAndy  
加好友时请备注  来自qtcn
非工作时间 可以电联 18610042411
离线liuchangyin

只看该作者 2楼 发表于: 2017-12-16
离线stlcours

只看该作者 3楼 发表于: 2017-12-21
请教一下楼主,或者各位大家,Qt客户端上传的代码已经没有问题了,那么HTTP Server的代码应该怎么写(怎么配置)呢?可以用Qt写吗?
在线夏德红

只看该作者 4楼 发表于: 2017-12-26
不错
离线lsyzsl

只看该作者 5楼 发表于: 2017-12-27
请问楼主,可以给个完整例子么?
离线q小玩家

只看该作者 6楼 发表于: 01-18
楼主请问报错"Error transferring http://192.168.0.1:180 - server replied: Method Not Allowed"是什么情况
快速回复
限100 字节
 
上一个 下一个