Error The program has unexpectedly finished while uploading multiple files
Unsolved
General and Desktop
-
I want to upload multiple files on server here is my code but using this code application crashed
QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType); QHttpPart imagePart; imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("text/plain")); QString uploadFile = uploadLine->text(); if(! uploadFile.isEmpty()) { QDirIterator UploadFolderFiles(uploadFile, QDir::Files | QDir::NoDotAndDotDot | QDir::NoSymLinks, QDirIterator::Subdirectories); while (UploadFolderFiles.hasNext()) { //get the files QString currentFile = UploadFolderFiles.next(); qDebug() << currentFile; imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data;name=usersfilename; filename="+currentFile)); QHttpPart textPart; textPart.setBody("file"); QFile *file = new QFile(currentFile); file->open(QIODevice::ReadOnly); imagePart.setBodyDevice(file); file->setParent(multiPart); multiPart->append(textPart); multiPart->append(imagePart); QString uploadDocUrl = docxProLine->text(); QString clientToken = tokenLine->text(); QUrl url(uploadDocUrl + "?ClientToken="+clientToken); QNetworkRequest request(url); qDebug() <<url; QNetworkAccessManager *networkManager= new QNetworkAccessManager; QNetworkReply * reply = networkManager->post(request, multiPart); multiPart->setParent(reply); // delete the multiPart with the reply qDebug() <<reply; if (reply->error() == QNetworkReply::NoError) { QString strReply = (QString)reply->readAll(); //parse json qDebug() << "Response:" << strReply; QJsonDocument jsonResponse = QJsonDocument::fromJson(strReply.toUtf8()); QJsonObject jsonObj = jsonResponse.object(); QJsonObject result=jsonObj["result"].toObject(); if(jsonObj["error"].toBool()==false) { QJsonObject data=result["data"].toObject(); QMessageBox::information(this, tr("Uploaded Successful"), tr("\"%1\"uploaded successfully").arg("File")); } else { QErrorMessage msg; msg.showMessage("Error during upload proces"); msg.exec(); } delete reply; } }//while ends here }//if ends here null else{ }
-
I want to upload multiple files on server here is my code but using this code application crashed
QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType); QHttpPart imagePart; imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("text/plain")); QString uploadFile = uploadLine->text(); if(! uploadFile.isEmpty()) { QDirIterator UploadFolderFiles(uploadFile, QDir::Files | QDir::NoDotAndDotDot | QDir::NoSymLinks, QDirIterator::Subdirectories); while (UploadFolderFiles.hasNext()) { //get the files QString currentFile = UploadFolderFiles.next(); qDebug() << currentFile; imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data;name=usersfilename; filename="+currentFile)); QHttpPart textPart; textPart.setBody("file"); QFile *file = new QFile(currentFile); file->open(QIODevice::ReadOnly); imagePart.setBodyDevice(file); file->setParent(multiPart); multiPart->append(textPart); multiPart->append(imagePart); QString uploadDocUrl = docxProLine->text(); QString clientToken = tokenLine->text(); QUrl url(uploadDocUrl + "?ClientToken="+clientToken); QNetworkRequest request(url); qDebug() <<url; QNetworkAccessManager *networkManager= new QNetworkAccessManager; QNetworkReply * reply = networkManager->post(request, multiPart); multiPart->setParent(reply); // delete the multiPart with the reply qDebug() <<reply; if (reply->error() == QNetworkReply::NoError) { QString strReply = (QString)reply->readAll(); //parse json qDebug() << "Response:" << strReply; QJsonDocument jsonResponse = QJsonDocument::fromJson(strReply.toUtf8()); QJsonObject jsonObj = jsonResponse.object(); QJsonObject result=jsonObj["result"].toObject(); if(jsonObj["error"].toBool()==false) { QJsonObject data=result["data"].toObject(); QMessageBox::information(this, tr("Uploaded Successful"), tr("\"%1\"uploaded successfully").arg("File")); } else { QErrorMessage msg; msg.showMessage("Error during upload proces"); msg.exec(); } delete reply; } }//while ends here }//if ends here null else{ }
@developerNancy Here is the Error
QObject::setParent: Cannot set parent, new parent is in a different thread
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
The program has unexpectedly finished. -
First of all, don't create a new
QNetworkAccessManager
for each file. Create oneQNetworkAccessManager
only, and use that to upload all your files.If that still doesn't resolve your issue, post your updated code here.