FenyaHere
FenyaHere

Reputation: 11

Qt 6.5.2 multipart upload stops after uploading the first chunk, works fine with Qt5

I'm trying to move from qt 5.15.2 to qt 6.5.2 and i have issues with uploading files to remote server. The following code works fine in qt5, but gets unknown network error in qt 6 after sending the first chunk. Works file small enough file. I've tried changing SSL protocols, Http2AllowedAttribute, nothing seems to be working. Any help is appriciated! Thank you!

output QT5:

Headers ("Authorization", "Content-Type", "MIME-Version", "Content-Length")
Content-length "75023"
Mime-Version "1.0"
Content-Type "multipart/form-data; boundary="ffeeffeeffee""
encrypted
Uploading "16384" / "75023"
Uploading "75023" / "75023"
Uploading "0" / "0"
"SSL: 7 4"
"Reply error: 0, code: 200, reply text: "
File object destroyed

Output QT6:

Headers QList("Authorization", "Content-Type", "MIME-Version", "Content-Length")
Content-length "75023"
Mime-Version "1.0"
Content-Type "multipart/form-data; boundary="ffeeffeeffee""
encrypted
Uploading "16384" / "75023"
Uploading "0" / "0"
"SSL: 4 2"
"Reply error: 99, code: 0, reply text: "
File object destroyed

Output QT6 with small file:

Headers QList("Authorization", "Content-Type", "MIME-Version", "Content-Length")
Content-length "2260"
Mime-Version "1.0"
Content-Type "multipart/form-data; boundary="ffeeffeeffee""
encrypted
Uploading "2260" / "2260"
Uploading "0" / "0"
"SSL: 4 2"
"Reply error: 0, code: 200, reply text: "
File object destroyed

Code:

QFile *file = new QFile(filepath);
if(!file->open(QIODevice::ReadOnly) || file->size() == 0){
      
} else 
{
  connect(file, &QObject::destroyed, this, [file]() { qDebug() << "File object destroyed"; } );

  QNetworkRequest request;
  QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
  multiPart->setBoundary("ffeeffeeffee");
  QHttpPart textPart;
  textPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant(QString("form-data; name=\"packet\"")));
  textPart.setRawHeader("Content-Type", "application/json");
  QJsonDocument dataDoc = QJsonDocument::fromVariant(jsonToSend);
  textPart.setBody(dataDoc.toJson());
  multiPart->append(textPart);
  QHttpPart filePart;
  request.setUrl(QUrl(m_uploadPicAddress));
  filePart.setRawHeader("Content-Type", "image/jpeg");
  filePart.setRawHeader("Content-Transfer-Encoding", "binary");


  filePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant(QString("form-data; name=\"image\"; filename=\"snapshot.jpg\"")));
  filePart.setBodyDevice(file);
  file->setParent(multiPart); 
  multiPart->append(filePart);
  QString headerData = "Basic " + authData;
  request.setRawHeader("Authorization", headerData.toLocal8Bit());
  QNetworkAccessManager *manager = new QNetworkAccessManager(this);
  manager->setRedirectPolicy(QNetworkRequest::ManualRedirectPolicy);
  request.setAttribute(QNetworkRequest::Http2AllowedAttribute, false);
  QNetworkReply* reply = manager->post(request, multiPart);

qDebug() << "Headers " << reply->request().rawHeaderList();
qDebug() << "Content-length " << reply->request().rawHeader("Content-Length");
qDebug() << "Mime-Version " << reply->request().rawHeader("MIME-Version");
qDebug() << "Content-Type " << reply->request().rawHeader("Content-Type");


  connect(reply, &QNetworkReply::uploadProgress, [](quint64 uploaded, quint64 total) {qDebug() << "Uploading" << QString::number(uploaded) << "/" << QString::number(total);});
  connect(reply, &QNetworkReply::redirected,
          this, [](const QUrl &url){qDebug() << url.toString();});
  connect(reply, &QNetworkReply::encrypted,
          this, [](){qDebug() << "encrypted";});
 /* connect(reply, &QNetworkReply::socketStartedConnecting,
          this, [](){qDebug() << "socketStartedConnecting";});
  connect(reply, &QNetworkReply::requestSent,
          this, [](){qDebug("requestSent";});*/
  connect(reply, &QNetworkReply::preSharedKeyAuthenticationRequired,
          this, [](QSslPreSharedKeyAuthenticator *authenticator){qDebug() << "auth req";});
  connect(reply, &QNetworkReply::errorOccurred,
          this, &DataUploaderAis::onErrorOccuredMedia);
  connect(reply, &QNetworkReply::sslErrors,
          this, &DataUploaderAis::onErrorOccuredMediaSsl);
  multiPart->setParent(reply); // delete the multiPart with the reply
  connect(reply, &QNetworkReply::finished, this,
          [this, manager, path, filename, reply]()
  {
    qDebug() << "SSL: " + QString::number(reply->sslConfiguration().protocol()) + " " +  QString::number(reply->sslConfiguration().sessionProtocol());
    auto replyError = reply->error();
    int code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
    QString replyText = reply->readAll();
     qDebug() << QString("Reply error: %1, code: %2, reply text: %3").arg(replyError).arg(code).arg(replyText);

    reply->close();
    reply->deleteLater();
    manager->deleteLater();
  });

Upvotes: 1

Views: 97

Answers (0)

Related Questions