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. Uploading Directory/Folder to a server

Uploading Directory/Folder to a server

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 2 Posters 506 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.
  • P Offline
    P Offline
    pingal
    wrote on last edited by pingal
    #1

    I've wrote a class for uploading/downloading files using QNetworkAccessManager and its working fine but now I want to add "uploading directory" functionality in it.

    Here is how I'm uploading a single file

    void HTTP::UploadFile (QNetworkRequest request, QFileInfo filePath){
    
        QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
        QHttpPart dataPart;
        dataPart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application"));
        dataPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=file; filename=\"" + filePath.fileName() + "\""));
        QFile *file = new QFile(filePath.absoluteFilePath());
        file->open(QIODevice::ReadOnly);
        dataPart.setBodyDevice(file);
        file->setParent(multiPart);
        multiPart->append(dataPart);
    
        QNetworkReply *reply = UploadManager.post(request, multiPart);
        connect(reply, &QNetworkReply::uploadProgress, this, &HTTP::showProgress);
    }
    

    Although I can iterate through files (within the directory) and upload individual files like below:-

    QDirIterator it(DirPath, QStringList() << "*.*", QDir::Files, QDirIterator::Subdirectories);
    while (it.hasNext())
        UploadFile (request, QFileInfo {it.next()});
    

    But it will just send files and NOT the directory structure. I want the files to be uploaded along the subdirectories.

    For example; If i want to upload a ParentDir. I would want to upload the ParentDir along with its subdirectories and files.

    How can I achieve that ?

    JonBJ 1 Reply Last reply
    0
    • P pingal

      I've wrote a class for uploading/downloading files using QNetworkAccessManager and its working fine but now I want to add "uploading directory" functionality in it.

      Here is how I'm uploading a single file

      void HTTP::UploadFile (QNetworkRequest request, QFileInfo filePath){
      
          QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
          QHttpPart dataPart;
          dataPart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application"));
          dataPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=file; filename=\"" + filePath.fileName() + "\""));
          QFile *file = new QFile(filePath.absoluteFilePath());
          file->open(QIODevice::ReadOnly);
          dataPart.setBodyDevice(file);
          file->setParent(multiPart);
          multiPart->append(dataPart);
      
          QNetworkReply *reply = UploadManager.post(request, multiPart);
          connect(reply, &QNetworkReply::uploadProgress, this, &HTTP::showProgress);
      }
      

      Although I can iterate through files (within the directory) and upload individual files like below:-

      QDirIterator it(DirPath, QStringList() << "*.*", QDir::Files, QDirIterator::Subdirectories);
      while (it.hasNext())
          UploadFile (request, QFileInfo {it.next()});
      

      But it will just send files and NOT the directory structure. I want the files to be uploaded along the subdirectories.

      For example; If i want to upload a ParentDir. I would want to upload the ParentDir along with its subdirectories and files.

      How can I achieve that ?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @pingal
      You need to recurse down your source directory structure, calling UploadFile() for files. You need to test/get your QDirIterator to return directories as well as files. You may want to write an UploadDirectory() method. And of course you need to ensure you tell the server to put files in the appropriate directory there, as necessary.

      1 Reply Last reply
      0
      • P Offline
        P Offline
        pingal
        wrote on last edited by
        #3

        So it means that first I've to get only Directories/SubDir list and send it to the server (so that it can make those directories) and then send individual files to the appropriate location.

        JonBJ 1 Reply Last reply
        0
        • P pingal

          So it means that first I've to get only Directories/SubDir list and send it to the server (so that it can make those directories) and then send individual files to the appropriate location.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @pingal
          Not necessarily. That depends on your server side, where it puts the files you send it. I'm not even sure your server side will do "creating a hierarchy" for the files/file paths you send it, it might put them all "flat" in one directory. You must check that out.

          P 1 Reply Last reply
          0
          • JonBJ JonB

            @pingal
            Not necessarily. That depends on your server side, where it puts the files you send it. I'm not even sure your server side will do "creating a hierarchy" for the files/file paths you send it, it might put them all "flat" in one directory. You must check that out.

            P Offline
            P Offline
            pingal
            wrote on last edited by pingal
            #5

            @JonB I'm using python http-server for that purpose and it put all files in one directory. I think I've to create my own server and use php for handling files/directories.

            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