Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Error The program has unexpectedly finished while uploading multiple files
QtWS25 Last Chance

Error The program has unexpectedly finished while uploading multiple files

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 939 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    developerNancy
    wrote on last edited by
    #1

    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{
    
    
     }
    
    D 1 Reply Last reply
    0
    • D developerNancy

      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{
      
      
       }
      
      D Offline
      D Offline
      developerNancy
      wrote on last edited by
      #2

      @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.

      1 Reply Last reply
      0
      • JKSHJ Offline
        JKSHJ Offline
        JKSH
        Moderators
        wrote on last edited by
        #3

        First of all, don't create a new QNetworkAccessManager for each file. Create one QNetworkAccessManager only, and use that to upload all your files.

        If that still doesn't resolve your issue, post your updated code here.

        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

        1 Reply Last reply
        1

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved