QNetworkAccessManager:从串行QIODevice发布http multipart [英] QNetworkAccessManager: post http multipart from serial QIODevice
问题描述
我正在尝试使用QNetworkAccessManager将http多部分上传到专用服务器。
I'm trying to use QNetworkAccessManager to upload http multiparts to a dedicated server.
multipart包含一个描述正在上传的数据的JSON部分。
The multipart consists of a JSON part describing the data being uploaded.
从串行QIODevice读取数据,该QIODevice对数据进行加密。
The data is read from a serial QIODevice, which encrypts the data.
这是创建多部分请求的代码:
This is the code that creates the multipart request:
QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
QHttpPart metaPart;
metaPart.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
metaPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"metadata\""));
metaPart.setBody(meta.toJson());
multiPart->append(metaPart);
QHttpPart filePart;
filePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant(fileFormat));
filePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"file\""));
filePart.setBodyDevice(p_encDevice);
p_encDevice->setParent(multiPart); // we cannot delete the file now, so delete it with the multiPart
multiPart->append(filePart);
QNetworkAccessManager netMgr;
QScopedPointer<QNetworkReply> reply( netMgr.post(request, multiPart) );
multiPart->setParent(reply.data()); // delete the multiPart with the reply
如果p_encDevice是QFile的实例,则该文件上传很好。
If the p_encDevice is an instance of QFile, that file gets uploaded just fine.
如果使用专门的加密QIODevice(串行设备),则从我的自定义设备读取所有数据。但是QNetworkAccessManager :: post()没有完成(挂起)。
If the specialised encrypting QIODevice is used (serial device) then all of the data is read from my custom device. however QNetworkAccessManager::post() doesn't complete (hangs).
我在 QHttpPart :
if device是顺序的(例如套接字,但不是文件),
QNetworkAccessManager :: post()应该在设备有
发出finish()后调用。
if device is sequential (e.g. sockets, but not files), QNetworkAccessManager::post() should be called after device has emitted finished().
不幸的是我不知道怎么做。
Unfortunately I don't know how do that.
请指教。
编辑:
QIODevice根本没有完成()插槽。更重要的是,如果没有调用QNetworkAccessManager :: post(),那么从我的自定义IODevice读取根本不会发生,因此设备将无法发出这样的事件。 (赶上22?)
QIODevice doesn't have finished() slot at all. What's more, reading from my custom IODevice doesn't happen at all if QNetworkAccessManager::post() is not called and therefore the device wouldn't be able to emit such an event. (Catch 22?)
编辑2:
似乎QNAM做了根本不适用于顺序设备。请参阅讨论qt-project 。
It seems that QNAM does not work with sequential devices at all. See discussion on qt-project.
编辑3:
我设法欺骗QNAM让它认为它是从非顺序设备读取的,但寻求和重置功能阻止寻求。这将一直有效,直到QNAM实际上试图寻找。
I managed to "fool" QNAM to make it think that it is reading from non-sequential devices, but seek and reset functions prevent seeking. This will work until QNAM will actually try to seek.
bool AesDevice::isSequential() const
{
return false;
}
bool AesDevice::reset()
{
if (this->pos() != 0) {
return false;
}
return QIODevice::reset();
}
bool AesDevice::seek(qint64 pos)
{
if (this->pos() != pos) {
return false;
}
return QIODevice::seek(pos);
}
推荐答案
来自< a href =http://qt-project.org/forums/viewthread/25317/ =nofollow> qt-project 并通过检查源代码,似乎QNAM不能用于顺序一点都不文档和代码都是错误的。
From a separate discussion in qt-project and by inspecting the source code it seems that QNAM doesn't work with sequential at all. Both the documentation and code are wrong.
这篇关于QNetworkAccessManager:从串行QIODevice发布http multipart的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!