Sending a file to website through post request
-
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 suggestionQNetworkRequest req;
req.setUrl(url);QNetworkAccessManager networkAM;
QFile* myImage = new QFile("filePath")
if(myImage->open(QIODevice::ReadOnly))
{
networkAM.post(req, myImage);
}
@ -
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 ?
-
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)
-
^^ 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.
-
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.