Solved Issue in Uploading Image file using post method:
-
@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. 🤔
-
@J-Hilk : Thanks a lot for reply, got it running
There were couple of issues:- 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)); - 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 ?
- file = new QFile(filename);
-
@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...
-
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) -
@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...
-
@Christian-Ehrlicher : yes
-
@Kira
with asynchronous, Mr @Christian-Ehrlicher means thatreply = _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 yetSo in short use the signals +slot/lambdas for this :)
-
@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. -
@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.
-
@mrjj : Finally closing the issue. Hope there are no more issue.
Thanks everyone for their help :p -
As long as there is software, there will be issues ;)