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. How to upload a file to a server
Forum Updated to NodeBB v4.3 + New Features

How to upload a file to a server

Scheduled Pinned Locked Moved Unsolved General and Desktop
15 Posts 4 Posters 7.8k 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.
  • M mps298

    Hi everybody,

    I'm asking the community for some help.
    My task is to upload a file to an http server. I know about QNetworkAccess and put/post methods, but I'm apparently struggling with pointing correct pathes, names, addresses etc and can't request uploading correctly, therefore my app crashes.
    We have:

    1. A file named input.txt, its absolute path is D:\files\input.txt (Windows 10 64 bit);
    2. An http server with address, say123.abc.com.

    I need this input.txt file to get uploaded and appear at 123.abc.com/input.txt

    If it can be done without stuff like creating QByteArray or something, directly sending the txt file as is, it would be just brilliant.

    Please, give me some good example how I should code it. The Internet is only full of download examples, looks as if nobody uploads files)) really looking forward to your help.

    Many thanks and all the best!

    raven-worxR Offline
    raven-worxR Offline
    raven-worx
    Moderators
    wrote on last edited by
    #3

    @mps298
    do you mean form upload (like it would have been done from a HTML form)?
    If so take a look at QHttpMultiPart class

    --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
    If you have a question please use the forum so others can benefit from the solution in the future

    1 Reply Last reply
    0
    • SGaistS SGaist

      Hi,

      1. What does that server do ? Is it ftp/sftp/S3 ?
        Does it have an end point that supports upload ?

      As for the file appearing at that specific address, it's the responsibility of the server to provide it correctly so unless you're using something like cutelyst to implement the server side, it's not a Qt question.

      M Offline
      M Offline
      mps298
      wrote on last edited by mps298
      #4

      @SGaist said in How to upload a file to a server:

      1. What does that server do ? Is it ftp/sftp/S3 ?
        Does it have an end point that supports upload ?

      It’s Nginx http server. It supports uploading as much as I know

      What do you think my upload request should look like? Thank you

      raven-worxR 1 Reply Last reply
      0
      • M mps298

        @SGaist said in How to upload a file to a server:

        1. What does that server do ? Is it ftp/sftp/S3 ?
          Does it have an end point that supports upload ?

        It’s Nginx http server. It supports uploading as much as I know

        What do you think my upload request should look like? Thank you

        raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by
        #5

        @mps298 said in How to upload a file to a server:

        What do you think my upload request should look like?

        you should tell us!
        What do you want to achieve?! What application runs on the server side? ...

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        M 1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #6

          @mps298 said in How to upload a file to a server:

          It Nginx http server. It supports uploading as much as I know

          That's not enough, you need to know exactly how your server is expecting the upload request. Whether it's the nginx upload module or an application that is served by nginx, what type of request it should be, what's the URL the request should be sent to, etc.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          M 1 Reply Last reply
          0
          • raven-worxR raven-worx

            @mps298 said in How to upload a file to a server:

            What do you think my upload request should look like?

            you should tell us!
            What do you want to achieve?! What application runs on the server side? ...

            M Offline
            M Offline
            mps298
            wrote on last edited by mps298
            #7

            @raven-worx said in How to upload a file to a server:

            What do you want to achieve?!

            As I wrote at the very beginning,

            I need this input.txt file to get uploaded and appear at 123.abc.com/input.txt

            That’s all I need to do, whether somehow copy the file from my computer to the server or copy a text from my file to an empty input.txt file which exists on the server (I can create it on the server in advance if necessary)
            I’m sorry, I don’t know how else I might describe it.

            What application runs on the server side? ...

            Some python script is supposed to use this uploaded input.txt file, I don’t know details, this is not my job.

            raven-worxR 1 Reply Last reply
            0
            • SGaistS SGaist

              @mps298 said in How to upload a file to a server:

              It Nginx http server. It supports uploading as much as I know

              That's not enough, you need to know exactly how your server is expecting the upload request. Whether it's the nginx upload module or an application that is served by nginx, what type of request it should be, what's the URL the request should be sent to, etc.

              M Offline
              M Offline
              mps298
              wrote on last edited by
              #8

              @SGaist said in How to upload a file to a server:

              That's not enough, you need to know exactly how your server is expecting the upload request. Whether it's the nginx upload module

              Yes, it is, no additional services are used for uploading.

              1 Reply Last reply
              0
              • M mps298

                @raven-worx said in How to upload a file to a server:

                What do you want to achieve?!

                As I wrote at the very beginning,

                I need this input.txt file to get uploaded and appear at 123.abc.com/input.txt

                That’s all I need to do, whether somehow copy the file from my computer to the server or copy a text from my file to an empty input.txt file which exists on the server (I can create it on the server in advance if necessary)
                I’m sorry, I don’t know how else I might describe it.

                What application runs on the server side? ...

                Some python script is supposed to use this uploaded input.txt file, I don’t know details, this is not my job.

                raven-worxR Offline
                raven-worxR Offline
                raven-worx
                Moderators
                wrote on last edited by
                #9

                @mps298 said in How to upload a file to a server:

                Some python script is supposed to use this uploaded input.txt file, I don’t know details, this is not my job.

                then you need to clarify what that python script expects exactly.

                --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                If you have a question please use the forum so others can benefit from the solution in the future

                M 1 Reply Last reply
                0
                • raven-worxR raven-worx

                  @mps298 said in How to upload a file to a server:

                  Some python script is supposed to use this uploaded input.txt file, I don’t know details, this is not my job.

                  then you need to clarify what that python script expects exactly.

                  M Offline
                  M Offline
                  mps298
                  wrote on last edited by mps298
                  #10

                  @raven-worx, this script needs input.txt file to exist, it uses text from the file for some purpose.

                  I get a similar txt file from that server easily, just like that:

                  void MainWindow::startDownloadSlot()
                  {
                      downloadManager->get(QNetworkRequest(QUrl(serverAddress+downloadFileName)));
                  }
                  
                  void MainWindow::downloadFinishedSlot(QNetworkReply *reply)
                  {
                      if (reply->error())
                      {
                          const QString error = reply->errorString();
                          qDebug()<<error;
                          return;
                      }
                      const QByteArray content = reply->readAll();
                      ui->outputTextEdit->setPlainText(content);
                      reply->deleteLater();
                  }
                  

                  Why can't I upload a file back in a similar way?

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #11

                    As @raven-worx already suggested, use QHttpMultiPart to build your upload query and post it to your server upload path.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    2
                    • M Offline
                      M Offline
                      mps298
                      wrote on last edited by
                      #12

                      I tried doing as below, but it successfully crashes the app every time, sending even no error reply...

                      void MainWindow::startUploadSlot(QFile *file)
                      {
                          if (file->open(QIODevice::ReadOnly))
                          {
                              QHttpMultiPart multiPart(QHttpMultiPart::FormDataType);
                      
                              QByteArray textBody;   
                              while (!file->atEnd())
                              {
                                  textBody.append(file->readLine());
                              }
                      
                              QHttpPart textPart;
                              textPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"text\""));
                      
                              textPart.setBody(textBody);
                              multiPart.append(textPart);
                      
                              uploadManager->put(QNetworkRequest(QUrl(serverAddress)), &multiPart);
                      
                              file->close();
                              return;
                          }
                      }
                      
                      jsulmJ 1 Reply Last reply
                      0
                      • M mps298

                        I tried doing as below, but it successfully crashes the app every time, sending even no error reply...

                        void MainWindow::startUploadSlot(QFile *file)
                        {
                            if (file->open(QIODevice::ReadOnly))
                            {
                                QHttpMultiPart multiPart(QHttpMultiPart::FormDataType);
                        
                                QByteArray textBody;   
                                while (!file->atEnd())
                                {
                                    textBody.append(file->readLine());
                                }
                        
                                QHttpPart textPart;
                                textPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"text\""));
                        
                                textPart.setBody(textBody);
                                multiPart.append(textPart);
                        
                                uploadManager->put(QNetworkRequest(QUrl(serverAddress)), &multiPart);
                        
                                file->close();
                                return;
                            }
                        }
                        
                        jsulmJ Online
                        jsulmJ Online
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #13

                        @mps298 Are file and uploadManager valid pointers?
                        Did you try to debug you're code to find out where exactly it crashes?

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

                        M 1 Reply Last reply
                        1
                        • jsulmJ jsulm

                          @mps298 Are file and uploadManager valid pointers?
                          Did you try to debug you're code to find out where exactly it crashes?

                          M Offline
                          M Offline
                          mps298
                          wrote on last edited by
                          #14

                          @jsulm, yes, both pointers are valid.

                          If I transfer a default QUrl() to the post method, it works, no crashes. By "works" I mean it sends me at least an error reply "Protocol is unknown". But when I use the real QUrl, it crashes, but later, not when the post method is working, and I can step over it when debugging, too, but in 7-10 seconds my app crashes

                          1 Reply Last reply
                          0
                          • SGaistS Offline
                            SGaistS Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on last edited by
                            #15

                            What is that URL ?
                            What does your server log says ?
                            How is it configured ?

                            Interested in AI ? www.idiap.ch
                            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                            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