Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Sending a file to website through post request

    General and Desktop
    5
    17
    7588
    Loading More Posts
    • 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.
    • B
      b1gsnak3 last edited by

      Hello!

      I am encountering one problem in sending a file with post request. I have a url "www.example.com/sth.php" and I must send a post request with similar to php input type=file. I assumed that this should be in the form of a bytearray. However when I send my bytearray to the webserver nothing happens

      1 Reply Last reply Reply Quote 0
      • A
        AcerExtensa last edited by

        Why you didn't check the forum before posting? On the first page there is already [solved] topic about uploading file to the server. "QNetworkAccessManager uploading files":http://qt-project.org/forums/viewthread/11361/

        God is Real unless explicitly declared as Integer.

        1 Reply Last reply Reply Quote 0
        • B
          b1gsnak3 last edited by

          yes well that solution doesn't work for me :)

          1 Reply Last reply Reply Quote 0
          • raven-worx
            raven-worx Moderators last edited by

            [quote author="b1gsnak3" date="1365684789"]yes well that solution doesn't work for me :)[/quote]

            and what exactly doesn't work for you? The solution from the topic seems like a legit POST-request to me?
            Show us how your request looks like?

            --- 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 Reply Quote 0
            • B
              b1gsnak3 last edited by

              I have multiple parameters in my php.. for example a post would look like:

              a=upload&t=token&file=image

              1 Reply Last reply Reply Quote 0
              • raven-worx
                raven-worx Moderators last edited by

                if that's all the info you're providing i think nobody will be able to help you.

                What isn't working?
                How you you create the Request?
                etc.

                --- 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 Reply Quote 0
                • B
                  b1gsnak3 last edited by

                  @
                  QUrl url;
                  url.addQueryItem("a", "upload");
                  url.addQueryItem("t", token);
                  url.addQueryItem("file", pictureByteArray.toHex());

                  QNetworkRequest req;
                  req.setUrl("www.example.com/sth.php");

                  QNetworkAccessManager networkAM;
                  networkAM.post(req, url.encodedQuery());

                  connect(&networkAM, SIGNAL(finished(QNetworkReply ), this, SLOT(replyHandler(QNetworkReply)));
                  @

                  1 Reply Last reply Reply Quote 0
                  • K
                    KA51O last edited by

                    Just from having a look at the documentation of QNAM::post I can tell that this line can not work.
                    @
                    networkAM.post(req, url.encodedQuery());
                    @

                    1 Reply Last reply Reply Quote 0
                    • B
                      b1gsnak3 last edited by

                      Well.. This is weird as I get reply from server... Why do you say it will not work?

                      1 Reply Last reply Reply Quote 0
                      • K
                        KA51O last edited by

                        Ok. I have no experience with QNAM, but the doc just mentioned QIODevice, const QByteArray & and QHttpMultiPart * as candidates for the second argument of post. And since QUrl inherits from none of these I thought this could not work.

                        I would have tried something like this. But this is just a shot in the dark.
                        @
                        QUrl url ("www.example.com/sth.php");
                        url.addQueryItem("a", "upload");
                        url.addQueryItem("t", token);
                        //url.addQueryItem("file", pictureByteArray.toHex()); Not sure if this is needed with my suggestion

                        QNetworkRequest req;
                        req.setUrl(url);

                        QNetworkAccessManager networkAM;
                        QFile* myImage = new QFile("filePath")
                        if(myImage->open(QIODevice::ReadOnly))
                        {
                        networkAM.post(req, myImage);
                        }
                        @

                        1 Reply Last reply Reply Quote 0
                        • B
                          b1gsnak3 last edited by

                          QUrl::encodedQuery() returns a QByteArray

                          1 Reply Last reply Reply Quote 0
                          • K
                            KA51O last edited by

                            Ahh ok. Sry for the confusion I caused.

                            Edit: Maybe the problem is related to this comment from the docs of QNAM::post [quote]data must be open for reading and must remain valid until the finished() signal is emitted for this reply.[/quote]

                            The object returned by QUrl::encodedQuery goes out of scope after the function returns ?

                            1 Reply Last reply Reply Quote 0
                            • B
                              b1gsnak3 last edited by

                              That is not the problem.. I created a new QByteArray to store the return value but still nothing happens... I think I must create my own model for sending after sniffing what a post method in php sends to the server :( bleah got into software to escape web dev and now i have to do this... Thank you I will submit some code example after I finish (if I finish)

                              1 Reply Last reply Reply Quote 0
                              • K
                                KA51O last edited by

                                ^^ Completely agree on the web dev part. Maybe "the example in the QHtmlMultiPart doc":http://qt-project.org/doc/qt-4.8/qhttpmultipart.html#details can be helpful ? Anyways good luck.

                                1 Reply Last reply Reply Quote 0
                                • C
                                  Code_ReaQtor last edited by

                                  Try to use "FormPost":http://www.tuckdesign.com/sources/Qt

                                  I have a similar problem when I am starting to code for an API which includes a "HTML Form" for sending files to a server. I used QNAM::post()... tried QHttpMultiPart... but still they won't work. Found out that there is something wrong with QHttpMultiPart and it uses a different specification (RFC 2046).

                                  FormPost should work. I just modified some parts of it and it worked like a charm. You can visit my "site":https://github.com/Code-ReaQtor/libqsendspace/tree/master/qsendspace/src for reference.

                                  Please visit my open-source projects at https://github.com/Code-ReaQtor.

                                  1 Reply Last reply Reply Quote 0
                                  • B
                                    b1gsnak3 last edited by

                                    Ty very much I believe this is what I must use I think. xD Will try tomorrow today my head is pounding from head banging the keyboard ^^

                                    1 Reply Last reply Reply Quote 0
                                    • B
                                      b1gsnak3 last edited by

                                      Ok so QHttpMultiPart doesn't work (wish I would've checked the post sooner, before head banging my head with it, seeing as someone already tried using this). Will try your solution tomorrow, thank you Code_ReaQtor

                                      1 Reply Last reply Reply Quote 0
                                      • First post
                                        Last post