• 379阅读
  • 0回复

[提问]大佬们,帮帮忙!http上传图片错误 [复制链接]

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

只看楼主 倒序阅读 楼主  发表于: 03-30
回复本帖可获得10RMB金钱奖励!
每人最多可获奖2次,奖池剩余40RMB金钱 (中奖几率90%)
一直报:Required request part 'file' is not present
postman(测试工具)




我的代码

   foreach (const QFileInfo info, QDir(path).entryInfoList()) {
        QString name = info.fileName(); //文件名
        if (!name.startsWith("YT")) {
            continue;
        }
        ui->textEdit->append("读取到图片:" + name);


        //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", "");
        QByteArray 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);
        cryptographic.addData(str1.toLocal8Bit());
        QByteArray signature = cryptographic.result().toHex();
        ui->textEdit->append("signature:" + signature);




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


        QHttpPart paramPart;
        paramPart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("text/plain"));
        paramPart.setHeader(QNetworkRequest::ContentDispositionHeader,
                           QVariant("form-data;name=\"param\""));
        paramPart.setBody(param);


        QHttpPart signaturePart;
        signaturePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("text/plain"));
        signaturePart.setHeader(QNetworkRequest::ContentDispositionHeader,
                           QVariant("form-data;name=\"signature\""));
        paramPart.setBody(signature);


        QHttpPart imagePart;
        imagePart.setHeader(QNetworkRequest::ContentTypeHeader,
                            QVariant("image/jpeg"));
        imagePart.setHeader(QNetworkRequest::ContentDispositionHeader,
                            QString("form-data; name=\"file\"; filename=\"" + info.filePath() + "\""));
        QFile *file = new QFile(info.filePath());
        bool open = file->open(QIODevice::ReadOnly);
        ui->textEdit->append(QString("文件打开:%1").arg(open));
        imagePart.setBodyDevice(file);
        file->setParent(multiPart);


        multiPart->append(imagePart);
        multiPart->append(signaturePart);
        multiPart->append(paramPart);


        QNetworkAccessManager manager;
        QNetworkRequest request;
        request.setUrl(QUrl(url1));
        qDebug() << request.url();
        request.setHeader(QNetworkRequest::ContentTypeHeader,
                          QVariant("multipart/form-data"));
        QNetworkReply *reply = manager.post(request, multiPart);
        multiPart->setParent(reply);
        QEventLoop loop(this);
        connect(&manager, &QNetworkAccessManager::finished,
                &loop, &QEventLoop::quit);
        connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),
                this, SLOT(upLoadError(QNetworkReply::NetworkError)));
        loop.exec();


哎,死活不成功啊!!!
快速回复
限100 字节
 
上一个 下一个