T
T
tr1cks2018-03-23 09:57:44
Qt
tr1cks, 2018-03-23 09:57:44

Why do I get a "device not open" error when I send a QTemporaryFile via POST/multipart from QNetworkAccessManager?

First, a temporary file is created, then the content is written (yes, it is necessary through the FS and not memory), then it is sent by
a POST request.
If I do this, I get the error "QIODevice::read (QTemporaryFile, "/tmp/MyApplication.XM3751"): device not open":

QTemporaryFile* tempFile = new QTemporaryFile();
tempFile->open();
QZipWriter zip(tempFile);
...
zip.close();
...

QHttpMultiPart* multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
QHttpPart part;
part.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"data\""));
tempFile->open();
part.setBodyDevice(tempFile);

multiPart->append(part);
tempFile->setParent(multiPart);

QNetworkReply* reply = networkAccessManager.post(QNetworkRequest(QUrl("http://ya.ru")), multiPart);
multiPart->setParent(reply);

If you add a file not through part.setBodyDevice(tempFile), but use part.setBody(tempFile->readAll()), then everything is in order.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
tr1cks, 2018-03-24
@tr1cks

The problem was that QZipWriter closed the temporary file when exiting the function in its destructor.

J
Jacob E, 2018-03-24
@Zifix

And if you check the second line, the opening itself, what will it display?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question