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. Select folder and upload all files from folder to server
Forum Updated to NodeBB v4.3 + New Features

Select folder and upload all files from folder to server

Scheduled Pinned Locked Moved Solved General and Desktop
28 Posts 5 Posters 9.2k Views 3 Watching
  • 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 am trying to upload all folder files (data) to server and move uploaded file into sent folder

    I have done with single file selection Here is my code Now I select the folder and upload all files of folder.

      QErrorMessage msg;
       QString usersfilename = QFileDialog::getOpenFileName();
       QDir dir = QFileInfo(usersfilename).absoluteDir();
       qDebug() << dir;
       qDebug() << usersfilename;
       this->ui->uploadEdit->setText(usersfilename);
         QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
        QHttpPart imagePart;
        imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("text/plain"));
      
        imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data;name=usersfilename;filename="+usersfilename));
        QHttpPart textPart;
    
        textPart.setBody("file");
    
    
    QFile *file = new QFile(usersfilename);
        file->open(QIODevice::ReadOnly);
        imagePart.setBodyDevice(file);
        file->setParent(multiPart); 
         multiPart->append(textPart);
        multiPart->append(imagePart);
    
        QUrl url("my url");
        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();
                  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 msgBox;
                      msgBox.setText("model is uploaded");
                      msgBox.exec();
                  }
                  else
                  {
                      QErrorMessage msg;
                      msg.showMessage("Error during upload proces");
                      msg.exec();
                  }
                  delete reply;
    
              }
    }
    
    K 1 Reply Last reply
    0
    • D developerNancy

      I am trying to upload all folder files (data) to server and move uploaded file into sent folder

      I have done with single file selection Here is my code Now I select the folder and upload all files of folder.

        QErrorMessage msg;
         QString usersfilename = QFileDialog::getOpenFileName();
         QDir dir = QFileInfo(usersfilename).absoluteDir();
         qDebug() << dir;
         qDebug() << usersfilename;
         this->ui->uploadEdit->setText(usersfilename);
           QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
          QHttpPart imagePart;
          imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("text/plain"));
        
          imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data;name=usersfilename;filename="+usersfilename));
          QHttpPart textPart;
      
          textPart.setBody("file");
      
      
      QFile *file = new QFile(usersfilename);
          file->open(QIODevice::ReadOnly);
          imagePart.setBodyDevice(file);
          file->setParent(multiPart); 
           multiPart->append(textPart);
          multiPart->append(imagePart);
      
          QUrl url("my url");
          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();
                    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 msgBox;
                        msgBox.setText("model is uploaded");
                        msgBox.exec();
                    }
                    else
                    {
                        QErrorMessage msg;
                        msg.showMessage("Error during upload proces");
                        msg.exec();
                    }
                    delete reply;
      
                }
      }
      
      K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      @developerNancy

      It might be helpful to tell also others what kind of problem you have with this, since you have posted it as a question.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      2
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by mrjj
        #3

        Hi

        To get all files in the folder you can do ( and sub folders...)

        QDirIterator it(dir, QStringList() << "*.*", QDir::Files, QDirIterator::Subdirectories);
        while (it.hasNext())
            qDebug() << it.next();  // this just lists them. you should move upload code to function and call it pr file.
        
        
        D 1 Reply Last reply
        2
        • mrjjM mrjj

          Hi

          To get all files in the folder you can do ( and sub folders...)

          QDirIterator it(dir, QStringList() << "*.*", QDir::Files, QDirIterator::Subdirectories);
          while (it.hasNext())
              qDebug() << it.next();  // this just lists them. you should move upload code to function and call it pr file.
          
          
          D Offline
          D Offline
          developerNancy
          wrote on last edited by
          #4

          @mrjj mainwindow.cpp:131: error: no matching function for call to 'QDirIterator::QDirIterator(QDir&, QStringList&, QDir::Filter, QDirIterator::IteratorFlag)'
          QDirIterator it(dir, QStringList() << ".", QDir::Files, QDirIterator::Subdirectories);

          issue after including QDir
          ^

          K 1 Reply Last reply
          0
          • D developerNancy

            @mrjj mainwindow.cpp:131: error: no matching function for call to 'QDirIterator::QDirIterator(QDir&, QStringList&, QDir::Filter, QDirIterator::IteratorFlag)'
            QDirIterator it(dir, QStringList() << ".", QDir::Files, QDirIterator::Subdirectories);

            issue after including QDir
            ^

            K Offline
            K Offline
            koahnig
            wrote on last edited by
            #5

            @developerNancy

            Did you include QDirIterator header?

            Vote the answer(s) that helped you to solve your issue(s)

            D 1 Reply Last reply
            0
            • K koahnig

              @developerNancy

              Did you include QDirIterator header?

              D Offline
              D Offline
              developerNancy
              wrote on last edited by developerNancy
              #6

              @koahnig Yes

              K 1 Reply Last reply
              0
              • D developerNancy

                @koahnig Yes

                K Offline
                K Offline
                koahnig
                wrote on last edited by
                #7

                @developerNancy

                You need to post a larger section of the code where the problem is.
                Also a larger stretch of the error messages are helpful.

                What is the OS and compiler you are using?

                Vote the answer(s) that helped you to solve your issue(s)

                D 1 Reply Last reply
                0
                • K koahnig

                  @developerNancy

                  You need to post a larger section of the code where the problem is.
                  Also a larger stretch of the error messages are helpful.

                  What is the OS and compiler you are using?

                  D Offline
                  D Offline
                  developerNancy
                  wrote on last edited by
                  #8

                  @koahnig I am using ubuntu I have already posted the code to upload file on server and for select the single file my code is void

                    void MainWindow::selectFileDownload()
                   {
                  
                       QString downloadlocation = QFileDialog::getOpenFileName();
                      QDir dir = QFileInfo(downloadlocation).absoluteDir();
                      qDebug() << dir;
                      qDebug() << downloadlocation;
                       this->downloadLine->setText(downloadlocation);
                  

                  }

                  jsulmJ 1 Reply Last reply
                  0
                  • D developerNancy

                    @koahnig I am using ubuntu I have already posted the code to upload file on server and for select the single file my code is void

                      void MainWindow::selectFileDownload()
                     {
                    
                         QString downloadlocation = QFileDialog::getOpenFileName();
                        QDir dir = QFileInfo(downloadlocation).absoluteDir();
                        qDebug() << dir;
                        qDebug() << downloadlocation;
                         this->downloadLine->setText(downloadlocation);
                    

                    }

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @developerNancy There is no such constructor. Please check the documentation: http://doc.qt.io/qt-5.8/qdiriterator.html

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    D 1 Reply Last reply
                    1
                    • jsulmJ jsulm

                      @developerNancy There is no such constructor. Please check the documentation: http://doc.qt.io/qt-5.8/qdiriterator.html

                      D Offline
                      D Offline
                      developerNancy
                      wrote on last edited by
                      #10

                      @jsulm I have not Implemented directory yet it is a selected file method.

                      D 1 Reply Last reply
                      0
                      • D developerNancy

                        @jsulm I have not Implemented directory yet it is a selected file method.

                        D Offline
                        D Offline
                        developerNancy
                        wrote on last edited by
                        #11

                        @developerNancy
                        Using this code
                        QDirIterator UploadFolderFiles(UploadFolder, QDirIterator::Subdirectories );
                        while (UploadFolderFiles.hasNext()) {
                        qDebug() << UploadFolderFiles.next();
                        }

                        My output is this:
                        /Desktop/testingQT"
                        /Desktop/testingQT/Linuxapp"
                        /Desktop/testingQT/.."
                        /Desktop/testingQT/."
                        /Desktop/testingQT/dfgdggd"

                        But I want only files.

                        mrjjM 1 Reply Last reply
                        0
                        • D developerNancy

                          @developerNancy
                          Using this code
                          QDirIterator UploadFolderFiles(UploadFolder, QDirIterator::Subdirectories );
                          while (UploadFolderFiles.hasNext()) {
                          qDebug() << UploadFolderFiles.next();
                          }

                          My output is this:
                          /Desktop/testingQT"
                          /Desktop/testingQT/Linuxapp"
                          /Desktop/testingQT/.."
                          /Desktop/testingQT/."
                          /Desktop/testingQT/dfgdggd"

                          But I want only files.

                          mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          @developerNancy

                          well u left out the list of files u want
                          so that seems to mean no files when u dont give it a list.

                          D 2 Replies Last reply
                          0
                          • mrjjM mrjj

                            @developerNancy

                            well u left out the list of files u want
                            so that seems to mean no files when u dont give it a list.

                            D Offline
                            D Offline
                            developerNancy
                            wrote on last edited by
                            #13

                            @mrjj My output is this:
                            /Desktop/testingQT"
                            /Desktop/testingQT/Linuxapp"
                            /Desktop/testingQT/.."
                            /Desktop/testingQT/."
                            /Desktop/testingQT/dfgdggd"

                            But there is . and .. also I want only files

                            1 Reply Last reply
                            0
                            • mrjjM mrjj

                              @developerNancy

                              well u left out the list of files u want
                              so that seems to mean no files when u dont give it a list.

                              D Offline
                              D Offline
                              developerNancy
                              wrote on last edited by
                              #14

                              @mrjj I want files only without . and .. means 1st is file name and second null and third also null and fourth is file name.

                              /Desktop/testingQT/Linuxapp"
                              /Desktop/testingQT/.."
                              /Desktop/testingQT/."
                              /Desktop/testingQT/dfgdggd"

                              the_T 1 Reply Last reply
                              0
                              • D developerNancy

                                @mrjj I want files only without . and .. means 1st is file name and second null and third also null and fourth is file name.

                                /Desktop/testingQT/Linuxapp"
                                /Desktop/testingQT/.."
                                /Desktop/testingQT/."
                                /Desktop/testingQT/dfgdggd"

                                the_T Offline
                                the_T Offline
                                the_
                                wrote on last edited by
                                #15

                                @developerNancy
                                You may have a look at http://doc.qt.io/qt-5/qdiriterator.html#QDirIterator-2 and http://doc.qt.io/qt-5/qdir.html#Filter-enum

                                -- No support in PM --

                                D 1 Reply Last reply
                                1
                                • the_T the_

                                  @developerNancy
                                  You may have a look at http://doc.qt.io/qt-5/qdiriterator.html#QDirIterator-2 and http://doc.qt.io/qt-5/qdir.html#Filter-enum

                                  D Offline
                                  D Offline
                                  developerNancy
                                  wrote on last edited by
                                  #16

                                  @the_ I am new in qt I don't know how to implement too confused

                                  the_T 1 Reply Last reply
                                  0
                                  • D developerNancy

                                    @the_ I am new in qt I don't know how to implement too confused

                                    the_T Offline
                                    the_T Offline
                                    the_
                                    wrote on last edited by
                                    #17

                                    @developerNancy
                                    Which part of the documentation don't you understand and what did you try so far?

                                    -- No support in PM --

                                    D 1 Reply Last reply
                                    0
                                    • the_T the_

                                      @developerNancy
                                      Which part of the documentation don't you understand and what did you try so far?

                                      D Offline
                                      D Offline
                                      developerNancy
                                      wrote on last edited by
                                      #18

                                      @the_ I have almost done but I want only files......

                                      the_T D 2 Replies Last reply
                                      0
                                      • D developerNancy

                                        @the_ I have almost done but I want only files......

                                        the_T Offline
                                        the_T Offline
                                        the_
                                        wrote on last edited by the_
                                        #19

                                        @developerNancy

                                        If you only want the files within a folder just apply the correct QDir::Filters to the QDirIterator.
                                        Just a guess: QDir::Files as can be found in http://doc.qt.io/qt-5/qdir.html#Filter-enum

                                        --EDIT

                                        QDir::NoDotAndDotDot is not neccessary if you use QDir::Files as . and .. are folders and not files. my bad, sorry

                                        -- No support in PM --

                                        D 1 Reply Last reply
                                        1
                                        • D developerNancy

                                          @the_ I have almost done but I want only files......

                                          D Offline
                                          D Offline
                                          developerNancy
                                          wrote on last edited by
                                          #20

                                          @developerNancy Would be great if you could provide me with some working example of this.

                                          Thanks

                                          1 Reply Last reply
                                          0

                                          • Login

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