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. Issue in Uploading Image file using post method:

Issue in Uploading Image file using post method:

Scheduled Pinned Locked Moved Solved General and Desktop
18 Posts 6 Posters 2.3k 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.
  • KiraK Offline
    KiraK Offline
    Kira
    wrote on last edited by
    #1

    Hello All,
    I am trying to upload an image file on the postman echo server using the post method. I can do it successfully using the postman application, as shown below.
    upload1.jpeg

    When I try to implement the functionality using Qt, I do not get any response from the server.
    Get request functionality work fine without any error, but there is no response for the post method please refer the below code:

    QUrl testUrl("https://postman-echo.com/post");
    QFile file("upload.jpeg");
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))            
    {
        qDebug() << "file open failure";   //send message if file cant open
    }
    file.close();
    QByteArray boundary; 
    QByteArray datas(QString("--" + boundary + "\r\n").toUtf8());
    datas += "Content-Disposition: form-data; name=\"file\"; filename=\"" + file.fileName() + "\"\r\n";
    datas += "Content-Type: image/jpeg\r\n\r\n"; //file type is here
    datas += "\r\n";
    datas += QString("--" + boundary + "\r\n\r\n").toUtf8();
    datas += "Content-Disposition: form-data; name=\"upload\"\r\n\r\n";
    datas += "Uploader\r\n";
    datas += QString("--" + boundary + "--\r\n").toUtf8();
    
    _request.setUrl(testUrl);  
    request.setRawHeader("Content-Type", "multipart/form-data; boundary=" + boundary); 
    _manager->post(_request, datas); //send all data
    

    Reference:
    https://doc.qt.io/archives/qt-4.8/qhttpmultipart.html
    https://www.qtcentre.org/threads/40734-Upload-file-to-HTTP-server

    JonBJ 1 Reply Last reply
    0
    • KiraK Kira

      Hello All,
      I am trying to upload an image file on the postman echo server using the post method. I can do it successfully using the postman application, as shown below.
      upload1.jpeg

      When I try to implement the functionality using Qt, I do not get any response from the server.
      Get request functionality work fine without any error, but there is no response for the post method please refer the below code:

      QUrl testUrl("https://postman-echo.com/post");
      QFile file("upload.jpeg");
      if (!file.open(QIODevice::ReadOnly | QIODevice::Text))            
      {
          qDebug() << "file open failure";   //send message if file cant open
      }
      file.close();
      QByteArray boundary; 
      QByteArray datas(QString("--" + boundary + "\r\n").toUtf8());
      datas += "Content-Disposition: form-data; name=\"file\"; filename=\"" + file.fileName() + "\"\r\n";
      datas += "Content-Type: image/jpeg\r\n\r\n"; //file type is here
      datas += "\r\n";
      datas += QString("--" + boundary + "\r\n\r\n").toUtf8();
      datas += "Content-Disposition: form-data; name=\"upload\"\r\n\r\n";
      datas += "Uploader\r\n";
      datas += QString("--" + boundary + "--\r\n").toUtf8();
      
      _request.setUrl(testUrl);  
      request.setRawHeader("Content-Type", "multipart/form-data; boundary=" + boundary); 
      _manager->post(_request, datas); //send all data
      

      Reference:
      https://doc.qt.io/archives/qt-4.8/qhttpmultipart.html
      https://www.qtcentre.org/threads/40734-Upload-file-to-HTTP-server

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

      @Kira
      Start by looking through the QNetworkAccessManager signals and attach to things like finished & sslErrors, are you getting any information?

      KiraK 1 Reply Last reply
      1
      • JonBJ JonB

        @Kira
        Start by looking through the QNetworkAccessManager signals and attach to things like finished & sslErrors, are you getting any information?

        KiraK Offline
        KiraK Offline
        Kira
        wrote on last edited by Kira
        #3

        @JonB : Currently I am using the following :

        QObject::connect(_manager, SIGNAL(finished(QNetworkReply*)),
               this, SLOT(managerFinished(QNetworkReply*)));
        

        But the signal is not being triggered i have put breakpoint in my slot it is not being invoked
        Can you please try connecting to the following https://postman-echo.com/post
        it up and running.
        Thanks

        JonBJ 1 Reply Last reply
        0
        • KiraK Kira

          @JonB : Currently I am using the following :

          QObject::connect(_manager, SIGNAL(finished(QNetworkReply*)),
                 this, SLOT(managerFinished(QNetworkReply*)));
          

          But the signal is not being triggered i have put breakpoint in my slot it is not being invoked
          Can you please try connecting to the following https://postman-echo.com/post
          it up and running.
          Thanks

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

          @Kira
          So maybe you are getting errors or authentication requests as per the other signals just like I suggested. Is there any reason you cannot write the code to check rather than not doing so?

          KiraK 2 Replies Last reply
          1
          • JonBJ JonB

            @Kira
            So maybe you are getting errors or authentication requests as per the other signals just like I suggested. Is there any reason you cannot write the code to check rather than not doing so?

            KiraK Offline
            KiraK Offline
            Kira
            wrote on last edited by
            #5

            @JonB : Sorry I didn't mean that :p
            Will check and let u know

            1 Reply Last reply
            1
            • JonBJ JonB

              @Kira
              So maybe you are getting errors or authentication requests as per the other signals just like I suggested. Is there any reason you cannot write the code to check rather than not doing so?

              KiraK Offline
              KiraK Offline
              Kira
              wrote on last edited by
              #6

              @JonB :
              Hey, apologies for the late reply.
              I analyzed the debug logs carefully and figured there was
              an issue with QSslSocket, unable to resolve things. On further digging down the problem, I came to know that run time SSL libraries were missing. I downloaded the DLLs based on the build version, placed them in the .exe folder, and everything started working.

              KiraK 1 Reply Last reply
              5
              • KiraK Kira

                @JonB :
                Hey, apologies for the late reply.
                I analyzed the debug logs carefully and figured there was
                an issue with QSslSocket, unable to resolve things. On further digging down the problem, I came to know that run time SSL libraries were missing. I downloaded the DLLs based on the build version, placed them in the .exe folder, and everything started working.

                KiraK Offline
                KiraK Offline
                Kira
                wrote on last edited by
                #7

                @Kira :
                Hello, everyone reopening the issue because of the related problem.
                I am trying to upload a jpeg file using QHttpMultipart the program is as given below:

                 QFile* file;
                    foreach(QString filename, images)
                    {
                        //QFile file = new QFile("C:/Aeonscan/Studies/2.jpeg");
                        file = new QFile(filename);
                        file->open(QIODevice::ReadOnly );
                        qDebug() << "File path:" << filename;
                        QHttpMultiPart* multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
                        QHttpPart imagePart;
                        //imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("text/plain"));
                        imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"file\"; filename=\"" + file->fileName() + ""));
                
                        //QHttpPart textPart;
                        //textPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"name\""));
                        //textPart.setBody("toto");/* toto is the name I give to my file in the server */
                
                        //QString apkLocation = "Test";
                        imagePart.setBodyDevice(file);
                        file->setParent(multiPart); // we cannot delete the file now, so delete it with the multiPart
                
                        //multiPart->append(textPart);
                        multiPart->append(imagePart);
                
                        QNetworkRequest request;
                        request.setUrl(testUrl);
                        //_manager->post(request, multiPart);
                        multiPart->setParent(_manager->post(request, multiPart));
                        file->close();
                

                I am getting the following error
                QIODevice::read (QFile, "upload.jpeg"): device not open
                QIODevice::read (QFile, "2.jpeg"): device not open
                And get the following error message:
                Error message: QNetworkReply::NetworkError(UnknownNetworkError)
                But when I replace the following line
                file = new QFile(filename);
                with the following line:
                QFile file = new QFile("C:/Studies/2.jpeg");
                everything works fine without any error.

                J.HilkJ 1 Reply Last reply
                0
                • KiraK Kira

                  @Kira :
                  Hello, everyone reopening the issue because of the related problem.
                  I am trying to upload a jpeg file using QHttpMultipart the program is as given below:

                   QFile* file;
                      foreach(QString filename, images)
                      {
                          //QFile file = new QFile("C:/Aeonscan/Studies/2.jpeg");
                          file = new QFile(filename);
                          file->open(QIODevice::ReadOnly );
                          qDebug() << "File path:" << filename;
                          QHttpMultiPart* multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
                          QHttpPart imagePart;
                          //imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("text/plain"));
                          imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"file\"; filename=\"" + file->fileName() + ""));
                  
                          //QHttpPart textPart;
                          //textPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"name\""));
                          //textPart.setBody("toto");/* toto is the name I give to my file in the server */
                  
                          //QString apkLocation = "Test";
                          imagePart.setBodyDevice(file);
                          file->setParent(multiPart); // we cannot delete the file now, so delete it with the multiPart
                  
                          //multiPart->append(textPart);
                          multiPart->append(imagePart);
                  
                          QNetworkRequest request;
                          request.setUrl(testUrl);
                          //_manager->post(request, multiPart);
                          multiPart->setParent(_manager->post(request, multiPart));
                          file->close();
                  

                  I am getting the following error
                  QIODevice::read (QFile, "upload.jpeg"): device not open
                  QIODevice::read (QFile, "2.jpeg"): device not open
                  And get the following error message:
                  Error message: QNetworkReply::NetworkError(UnknownNetworkError)
                  But when I replace the following line
                  file = new QFile(filename);
                  with the following line:
                  QFile file = new QFile("C:/Studies/2.jpeg");
                  everything works fine without any error.

                  J.HilkJ Online
                  J.HilkJ Online
                  J.Hilk
                  Moderators
                  wrote on last edited by
                  #8

                  @Kira seems like the QHttpPart does not make a deep copy of the file data, and your loop is invalidating the data before it can be fully processed. 🤔


                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  KiraK 1 Reply Last reply
                  0
                  • J.HilkJ J.Hilk

                    @Kira seems like the QHttpPart does not make a deep copy of the file data, and your loop is invalidating the data before it can be fully processed. 🤔

                    KiraK Offline
                    KiraK Offline
                    Kira
                    wrote on last edited by
                    #9

                    @J-Hilk : Thanks a lot for reply, got it running
                    There were couple of issues:

                    1. file = new QFile(filename);
                      was not getting complete file path while opening the file as filename had only names.
                      Replace it with:
                      file = new QFile(directory.filePath(filename));
                    2. file.close(); Removed the following line.
                      After that it got up and running.

                    Just for confirmation. I am getting code 200 in reply. If the file have been actually uploaded will i have to check the server for file or can be done in code ?

                    Pablo J. RoginaP 1 Reply Last reply
                    0
                    • KiraK Kira

                      @J-Hilk : Thanks a lot for reply, got it running
                      There were couple of issues:

                      1. file = new QFile(filename);
                        was not getting complete file path while opening the file as filename had only names.
                        Replace it with:
                        file = new QFile(directory.filePath(filename));
                      2. file.close(); Removed the following line.
                        After that it got up and running.

                      Just for confirmation. I am getting code 200 in reply. If the file have been actually uploaded will i have to check the server for file or can be done in code ?

                      Pablo J. RoginaP Offline
                      Pablo J. RoginaP Offline
                      Pablo J. Rogina
                      wrote on last edited by
                      #10

                      @Kira said in Issue in Uploading Image file using post method::

                      Just for confirmation. I am getting code 200 in reply.

                      See here for the meaning of that HTTP response code:

                      200 OK
                      The request has succeeded. The meaning of the success depends on the HTTP method:
                      ...
                      PUT or POST: The resource describing the result of the action is transmitted in the message body.

                      If the file have been actually uploaded will i have to check the server for file or can be done in code ?

                      Given you receive status code 200 you're done, the server is telling you the request was processed Ok.

                      What if the server your posting the files to is just all across the world? You won't be able to check the file was actually written to the server's file system.

                      After receiving the response Ok, you could GET the file from the server and compare it with the copy you have, but that's paranoid work it seems...

                      Upvote the answer(s) that helped you solve the issue
                      Use "Topic Tools" button to mark your post as Solved
                      Add screenshots via postimage.org
                      Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                      1 Reply Last reply
                      2
                      • KiraK Offline
                        KiraK Offline
                        Kira
                        wrote on last edited by Kira
                        #11

                        Hi everyone,
                        I have modified my code as follows:

                         QUrl            apiEndpoint(QString::fromStdString(_config->GetAPIEndpoint()));	
                            QDir            directory(QString::fromStdString(path));
                            QStringList     images = directory.entryList(QDir::Files);
                            QNetworkRequest request;
                            QNetworkReply*  reply;
                            QFile*          file;
                        
                            foreach(QString filename, images)
                            {
                                file = new QFile("C:\\Studies\\upload.jpeg");
                                file->open(QIODevice::ReadOnly );
                                QHttpMultiPart* multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
                                QHttpPart imagePart;
                                imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"file\"; filename=\"" + file->fileName() + ""));
                                imagePart.setBodyDevice(file);
                                file->setParent(multiPart);
                                multiPart->append(imagePart);
                                request.setUrl(apiEndpoint);
                                reply = _manager->post(request, multiPart);
                                qDebug() << "Reply" << reply->readAll();
                                qDebug() << "Error" << reply->error();
                                qDebug() << "Status" << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
                            }
                        

                        I am getting the following error:
                        Reply Error: QNetworkReply::NetworkError(NoError)

                        1 Reply Last reply
                        0
                        • Christian EhrlicherC Offline
                          Christian EhrlicherC Offline
                          Christian Ehrlicher
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          @Kira said in Issue in Uploading Image file using post method::

                          I am getting the following error:

                          And what's the problem now? The API is asynchronous...

                          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                          Visit the Qt Academy at https://academy.qt.io/catalog

                          KiraK 1 Reply Last reply
                          3
                          • Christian EhrlicherC Christian Ehrlicher

                            @Kira said in Issue in Uploading Image file using post method::

                            I am getting the following error:

                            And what's the problem now? The API is asynchronous...

                            KiraK Offline
                            KiraK Offline
                            Kira
                            wrote on last edited by
                            #13

                            @Christian-Ehrlicher : yes

                            mrjjM 1 Reply Last reply
                            0
                            • KiraK Kira

                              @Christian-Ehrlicher : yes

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

                              @Kira
                              with asynchronous, Mr @Christian-Ehrlicher means that

                              reply = _manager->post(request, multiPart); // start the post process. and do not wait until finished and goes to next line
                              qDebug() << "Reply" << reply->readAll(); // there might not be anything to read yet
                              qDebug() << "Error" << reply->error(); // might be no errors to report yet

                              So in short use the signals +slot/lambdas for this :)

                              KiraK 1 Reply Last reply
                              2
                              • mrjjM mrjj

                                @Kira
                                with asynchronous, Mr @Christian-Ehrlicher means that

                                reply = _manager->post(request, multiPart); // start the post process. and do not wait until finished and goes to next line
                                qDebug() << "Reply" << reply->readAll(); // there might not be anything to read yet
                                qDebug() << "Error" << reply->error(); // might be no errors to report yet

                                So in short use the signals +slot/lambdas for this :)

                                KiraK Offline
                                KiraK Offline
                                Kira
                                wrote on last edited by
                                #15

                                @mrjj : Thanks for the clarification.
                                What you said is correct.
                                I was getting error was because nothing was ready, neither response nor error.
                                The program I have written is asynchronous. I have used signals and slots, but the actual problem was I had not allocated memory on the heap while creating the class object.
                                I had created the object inside the QPushButton slot, and memory got allocated on the stack the object got destroyed before the response got
                                generated.
                                This scenario also brings me to a question:
                                If we allocate memory on the heap and response is generated when the object will get released from memory, or is it likely to cause a memory leak?
                                This scenario often occurs when allocating memory for the widgets. How the memory management gets done and when do we have to delete the objects manually.

                                mrjjM 1 Reply Last reply
                                0
                                • KiraK Kira

                                  @mrjj : Thanks for the clarification.
                                  What you said is correct.
                                  I was getting error was because nothing was ready, neither response nor error.
                                  The program I have written is asynchronous. I have used signals and slots, but the actual problem was I had not allocated memory on the heap while creating the class object.
                                  I had created the object inside the QPushButton slot, and memory got allocated on the stack the object got destroyed before the response got
                                  generated.
                                  This scenario also brings me to a question:
                                  If we allocate memory on the heap and response is generated when the object will get released from memory, or is it likely to cause a memory leak?
                                  This scenario often occurs when allocating memory for the widgets. How the memory management gets done and when do we have to delete the objects manually.

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

                                  @Kira
                                  Ah. ok. it was not clear from code.
                                  So it was out of scope issue.
                                  Well many Qt classes take a pointer to a parent. it will auto clean up any
                                  children it owns. ( like forms delete all its buttons etc )
                                  So in most cases, you dont have to do it manually.

                                  https://doc.qt.io/qt-5/objecttrees.html

                                  QNetworkReply being an exception i think.

                                  KiraK 1 Reply Last reply
                                  0
                                  • mrjjM mrjj

                                    @Kira
                                    Ah. ok. it was not clear from code.
                                    So it was out of scope issue.
                                    Well many Qt classes take a pointer to a parent. it will auto clean up any
                                    children it owns. ( like forms delete all its buttons etc )
                                    So in most cases, you dont have to do it manually.

                                    https://doc.qt.io/qt-5/objecttrees.html

                                    QNetworkReply being an exception i think.

                                    KiraK Offline
                                    KiraK Offline
                                    Kira
                                    wrote on last edited by
                                    #17

                                    @mrjj : Finally closing the issue. Hope there are no more issue.
                                    Thanks everyone for their help :p

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

                                      As long as there is software, there will be issues ;)

                                      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