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. Post multiple files with QNetworkRequest in Qt 6.4.2 C++ and php
Forum Updated to NodeBB v4.3 + New Features

Post multiple files with QNetworkRequest in Qt 6.4.2 C++ and php

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 449 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.
  • KaguroK Offline
    KaguroK Offline
    Kaguro
    wrote on last edited by Kaguro
    #1

    Hi Guys!
    I have a problem with uplode multiple files to my server. When I try to upload just one file its working, but in this way not, where is my mistake?

    Qt upload part:

    void FileUploader::uploadFile() 
    {
        QList<QFile *> *file = new QList<QFile *>;
        QStringList list ={"F:/Download/Heti hetes/2 evad/HH 2x42 2001-06-16e1.avi","F:/Download/Heti hetes/1 evad/bestofhh0.avi","F:/Download/Heti hetes/1 evad/h7_1999-11-27 (1).avi"};
        QStringList listname ={"video_name1","video_name2","video_name3"};
        for (int i = 0; i < list.size(); i++) 
        {
            QFile *tmp= new QFile(list.at(i));
            file->append(tmp);
        }
        QUrl url("http://xyz/upfiles.php");
        QNetworkRequest request(url);
    
        QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
        for (int i = 0; i < list.size(); i++)
        {
            QHttpPart filePart;
            filePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"file\"; filename=\""+ listname.at(i) +".mp4\""));
            file->at(i)->open(QIODevice::ReadOnly);
            filePart.setBodyDevice(file->at(i));
            file->at(i)->setParent(multiPart);
    
            multiPart->append(filePart);
        }
    
        QHttpPart typePart;
        typePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"type\""));
        typePart.setBody("video");
    
        multiPart->append(typePart);
        reply = networkManager->post(request, multiPart); 
        multiPart->setParent(reply);
    
        connect(reply, &QNetworkReply::uploadProgress, this, &FileUploader::uploadProgress);
        connect(reply, &QNetworkReply::finished, this, &FileUploader::onUploadFinished);
        connect(reply, &QNetworkReply::errorOccurred, this, &FileUploader::onError);
    }
    

    php part:

    <?php
    if ($_SERVER['REQUEST_METHOD'] == 'POST') 
    {
        foreach ($_FILES as $fileKey => $fileValue) 
        {
            if(isset($_POST['type']) && $_POST['type'] == "video") 
            {
                $target_dir = "videos/";
            } 
            else 
            {
                $target_dir = "thumbnail/";	
            }
    
            $target_file = $target_dir . basename($fileValue["name"]);
    
            if (move_uploaded_file($fileValue["tmp_name"], $target_file)) 
            {
                echo htmlspecialchars(basename($fileValue["name"])) . "Upload Success";
            } 
            else 
            {
                echo htmlspecialchars(basename($fileValue["name"])) . "Upload Fail";
            }
        }
    } 
    ?>
    

    Thanks for your help!

    1 Reply Last reply
    0
    • C ChrisW67

      What have you done to debug it?
      Have you sniffed the network traffic to see what went on the wire (both directions)?
      When it worked with one file was that one file in a multi-part upload, or some other mechanism?
      Is the first file about 6% of the total? What would that suggest?
      Does it work with many small files?
      What are the file upload limits at the PHP end (upload_max_filesize and max_file_uploads)?
      Is there enough temporary space on the server side (upload_tmp_dir)?
      What is recorded in logs on the server end?

      KaguroK Offline
      KaguroK Offline
      Kaguro
      wrote on last edited by
      #6

      @ChrisW67 Now its working perfectly! I thought that the file size only had to be set in php.ini, but no, it had to be set in apache.conf as well:
      LimitRequestBody 2147483648 (for example this is 2gb limit).
      So many thank you for your help and questions!:)

      1 Reply Last reply
      2
      • C Offline
        C Offline
        ChrisW67
        wrote on last edited by
        #2

        As you do not describe what "not working" means it will be difficult to get specific help. What does and does not work? What have you done to debug it? Have you sniffed the network traffic to see what went on the wire?

        At a glance you are not setting Content-Type on the binary parts.

        1 Reply Last reply
        1
        • KaguroK Offline
          KaguroK Offline
          Kaguro
          wrote on last edited by
          #3

          When I upload only 1 file in a similar way (I use post in the cycle), then it works what it means It uploads the files to the right place. (All files) If I want to send it to a post as I wrote in the question (All files in one post), it goes very slowly to 6%, and says "Upload error: QNetworkReply::UnknownNetworkError" in debug and the upload stops. But it didn't upload any of the files.

          1 Reply Last reply
          0
          • C Offline
            C Offline
            ChrisW67
            wrote on last edited by ChrisW67
            #4

            What have you done to debug it?
            Have you sniffed the network traffic to see what went on the wire (both directions)?
            When it worked with one file was that one file in a multi-part upload, or some other mechanism?
            Is the first file about 6% of the total? What would that suggest?
            Does it work with many small files?
            What are the file upload limits at the PHP end (upload_max_filesize and max_file_uploads)?
            Is there enough temporary space on the server side (upload_tmp_dir)?
            What is recorded in logs on the server end?

            KaguroK 1 Reply Last reply
            2
            • KaguroK Offline
              KaguroK Offline
              Kaguro
              wrote on last edited by Kaguro
              #5

              Okay, thanks for your questions it makes clear what the main problem is. I tried upload 50 mini files in this way, and it works. When I try to upload one 4,7 Gb file I got the same QNetworkReply::UnknownNetworkError and nothing happened (upload nothing). I thought the time is the problem, but no the size of ONE file! If its more then 1Gb the upload dies. I set everything on my server in php.ini (max_input_time = 600, memory_limit = 4G, post_max_size = 8G, max_file_uploads = 70) but the maximum 1gb file size never change.. no more no less than 1gb and the upload dies. Tmp dir size is okay( Size 98G Used 39G Avail 55G Use 41% Mounted on / ). Now I can't see the network traffic and this kind of logs, i am so beginner in ubuntu servers.
              So I think the problem is the size not the 30 sec timeout, but I have no idea where I have to change it.

              1 Reply Last reply
              0
              • C ChrisW67

                What have you done to debug it?
                Have you sniffed the network traffic to see what went on the wire (both directions)?
                When it worked with one file was that one file in a multi-part upload, or some other mechanism?
                Is the first file about 6% of the total? What would that suggest?
                Does it work with many small files?
                What are the file upload limits at the PHP end (upload_max_filesize and max_file_uploads)?
                Is there enough temporary space on the server side (upload_tmp_dir)?
                What is recorded in logs on the server end?

                KaguroK Offline
                KaguroK Offline
                Kaguro
                wrote on last edited by
                #6

                @ChrisW67 Now its working perfectly! I thought that the file size only had to be set in php.ini, but no, it had to be set in apache.conf as well:
                LimitRequestBody 2147483648 (for example this is 2gb limit).
                So many thank you for your help and questions!:)

                1 Reply Last reply
                2
                • KaguroK Kaguro has marked this topic as solved on
                • C Offline
                  C Offline
                  ChrisW67
                  wrote on last edited by
                  #7

                  I love it when a plan comes together

                  1 Reply Last reply
                  2

                  • Login

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