• 103阅读
  • 1回复

http 上传图片  急! [复制链接]

上一主题 下一主题
离线介农酥
 

只看楼主 倒序阅读 楼主  发表于: 03-26
回复本帖可获得10RMB金钱奖励!
每人最多可获奖2次,奖池剩余30RMB金钱 (中奖几率90%)
我这边需要通过http的方式把图片上传给服务器,但是服务器给出了报错:Required request part 'file' is not present,客户端报的错误:server replied: Internal Server Error。
在postMan中可以正确执行



我的代码:

        //file
        QImage map(info.filePath());
        QByteArray byteArray;
        QDataStream write(&byteArray, QIODevice::WriteOnly);
        write << map;
        QString fileBinary = QString::fromLocal8Bit(byteArray.toBase64());
        qDebug() << QString("%1大小%2").arg(name).arg(fileBinary.size());
        ui->textEdit->append(QString("%1大小%2").arg(name).arg(fileBinary.size()));
//上面的代码主要是把图片二进制数据读到字符串fileBinary


        //param
        QJsonObject object;
        QString expressNumber = name.section(QChar('_'), 0, 0);
        object.insert("expressNumber", expressNumber);
        object.insert("nameNumber", "574901"); //网点代号
        object.insert("opCode", "182"); //操作代码
        object.insert("snNumber", mac); //设备编码
        object.insert("imgSource", info.filePath()); //图片来源
        object.insert("extend", "");
        QString param = QJsonDocument(object).toJson();
        qDebug() << "param" << param;
        ui->textEdit->append("param:" + param);
        //signature
        int length = param.length();
        QString str = QString::asprintf("%08d", length);
        qDebug() << "param长度" << str;
        ui->textEdit->append("param长度" + str);
        QString str1 = str + param;
        QCryptographicHash cryptographic(QCryptographicHash::Md5);
        QString signature = cryptographic.result().toHex();
        ui->textEdit->append("signature:" + signature);
        //url
        QString url = QString("file=%1&signature=%2&param=%3")
        .arg(fileBinary).arg(signature).arg(param);
        qDebug() << url;

        //同步
        QEventLoop loop(this);
        QNetworkAccessManager manager;
        QNetworkRequest request;
        request.setUrl(QUrl(url1));
        qDebug() << request.url();
        request.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("multipart/form-data"));
        QNetworkReply *reply = manager.post(request, url.toUtf8());
        connect(&manager, &QNetworkAccessManager::finished, &loop, &QEventLoop::quit);
        loop.exec();

红色部分为关键部分,因为上面说是file这个错误,大家帮忙看看,这个file为什么错误,
离线liulin188

只看该作者 1楼 发表于: 03-27
回帖奖励+ 10
    
https://wiki.qt.io/Qt_5.12_Release
https://www.qt.io/blog/qt-5.13.2-released
https://www.qt.io/blog/qt-creator-4.10.2-released
快速回复
限100 字节
 
上一个 下一个