Select folder and upload all files from folder to server



  • 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;
    
              }
    }

  • Moderators

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


  • Qt Champions 2016

    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.
    
    


  • @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
    ^


  • Moderators

    @developerNancy

    Did you include QDirIterator header?



  • @koahnig Yes


  • Moderators

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



  • @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);
    

    }


  • Moderators

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



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



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


  • Qt Champions 2016

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



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



  • @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_ I am new in qt I don't know how to implement too confused



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



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



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



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

    Thanks



  • @the_ said what I have to change

          QString UploadFolder = QFileDialog::getExistingDirectory(this, tr("Open Directory"),"/home",QFileDialog::ShowDirsOnly| QFileDialog::DontResolveSymlinks);
    
      QDirIterator UploadFolderFiles(UploadFolder, QDirIterator::Subdirectories);
          while (UploadFolderFiles.hasNext()) {
              qDebug() << UploadFolderFiles.next();
          }
     this->uploadLine->setText(UploadFolder);


  • @developerNancy

    Just use QDirIterator::QDirIterator(const QString &path, QDir::Filters filters, IteratorFlags flags = NoIteratorFlags) instead of QDirIterator::QDirIterator(const QDir &dir, IteratorFlags flags = NoIteratorFlags) as your constructor and apply the filters you need as listed in QDir::Filters



  • @the_ Thanks....


  • Qt Champions 2016

    @developerNancy

    So in short, it is like first shown

    QDirIterator it(dir, QStringList() << "*.*", QDir::Files, QDirIterator::Subdirectories);
    

    The

    QStringList() << "*.*"
    

    tell what files you are after.

    If you dont tell it, it seems to think u say "i dont want any files" which makes sense.



  • @mrjj
    With the difference that *.* does only match files that contain a dot but misses all files without file extension ;)


  • Qt Champions 2016

    @the_
    Good point
    But will
    "*"
    match all files , even on windows ?



  • @mrjj
    Not 100% sure if it matches all files but should do with all visible files.



  • @the_ Its working using this QDirIterator::QDirIterator(const QString &path, QDir::Filters filters, IteratorFlags flags = NoIteratorFlags)


Log in to reply