I am currently using QHttpMultiPart in a Qt Project, but it seems to have some problems on my end ?
I have followed the example and came up with the following code:
#include "uploader.h"
#include <QFileInfo>
#include <QMimeDatabase>
#include <QHttpMultiPart>
#include <QNetworkReply>
#include <QDebug>
/**
* #brief Uploader::Uploader
* #param parent
*/
Uploader::Uploader(QObject *parent) :
QObject(parent)
{
uploadInProgress = false;
}
/**
* #brief Uploader::upload
* #param absoluteFilePath
*/
void Uploader::upload(QString absoluteFilePath)
{
qDebug() << "Upload Starting";
QFileInfo fileInfo(absoluteFilePath);
QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
//action part
QHttpPart textPart;
textPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"cmd\""));
textPart.setBody(QString("wFile").toLatin1());
//File Path
QHttpPart filePathPart;
filePathPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"file_path\""));
filePathPart.setBody(absoluteFilePath.toLatin1());
//filepart
QHttpPart filePart;
QMimeDatabase db;
QMimeType mime = db.mimeTypeForFile(absoluteFilePath);
filePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant(mime.name()));
filePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"preview_file\"; filename=\""+ fileInfo.baseName() + "\""));
QFile *file = new QFile(absoluteFilePath);
if ( !file->exists() )
{
qDebug() << "File Does not exist";
}
file->open(QIODevice::ReadOnly);
filePart.setBodyDevice(file);
file->setParent(multiPart); // we cannot delete the file now, so delete it with the multiPart
multiPart->append(textPart);
multiPart->append(filePathPart);
multiPart->append(filePart);
QUrl url("http://project.dbz.dev/index.php?controller=wapi&action=handle");
QNetworkRequest request(url);
pManager = new QNetworkAccessManager();
pReply = pManager->post(request, multiPart);
multiPart->setParent(pReply);
connect(pReply, SIGNAL(uploadProgress(qint64,qint64)),this,SLOT(uploadProgress(qint64,qint64)));
connect(pReply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(onError(QNetworkReply::NetworkError)));
connect(pReply, SIGNAL(finished()),this, SLOT(uploadFinished()));
// here connect signals etc.
uploadInProgress = true;
}
/**
* #brief Uploader::uploadFinished
*/
void Uploader::uploadFinished()
{
QString data = (QString) pReply->readAll();
qDebug() << data;
qDebug() << "Upload finished";
uploadInProgress = false;
if ( pReply->error() > 0 )
{
qDebug() << "Error occured: " << pReply->error() << " : " << pReply->errorString();
}
else
{
qDebug() << "Upload success";
}
delete pReply;
}
void Uploader::uploadProgress(qint64 a, qint64 b)
{
qDebug() << " SOME PROGRESS!";
qDebug() << a << "/" << b;
}
void Uploader::onError(QNetworkReply::NetworkError err)
{
qDebug() << " SOME ERROR!";
qDebug() << err;
}
Sadly, none of the SLOTS are triggered from the SIGNALS. Neither can I see a package send with wireshark on my local ethernet adapter.
However, my Apache does get a request:
192.168.178.21 - - [21/Sep/2013:05:10:41 +0200] "POST /index.php?controller=wapi&action=handle HTTP/1.1" 200 166 "-" "Mozilla/5.0"
And in my PHP Application I have the following outcome:
Application_Controller_WapiController::handleAction: Command: wFile
Application_Controller_WapiController::wFile: POST Request: 1
This, basically means, it recognises the Parameter "cmd" and the value "wFile", opens the according PHP action which then does a print_r($_POST) which shows me nothing more than a simple 1.
I have no idea what to do. I have looked everywhere on the internet and cannot seem to figure it out. I followed all examples and descriptions on the official documentary and found a couple of threads here on SO. There seemed to be a bug with the QHttpMultiPart class, although it was fixed with the major 5.0.0 update.
tl;dr:
connect(pReply, SIGNAL(uploadProgress(qint64,qint64)),this,SLOT(uploadProgress(qint64,qint64)));
connect(pReply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(onError(QNetworkReply::NetworkError)));
connect(pReply, SIGNAL(finished()),this, SLOT(uploadFinished()));
These signals are not being triggered, PHP print_r shows me a 1 and I cannot track the POST request on my machine.
It would be nice if somebody could tell me why the SIGNALS are not emitted and more importantly how I can see a final version of my POST request in my C++ application before it is sent.
Thank you very much! I appreciate any help!
I have solved the problem by adding:
pELoop = new QEventLoop();
pELoop->exec();
Which results into this:
#include "uploader.h"
#include <QFileInfo>
#include <QMimeDatabase>
#include <QHttpMultiPart>
#include <QNetworkReply>
#include <QDebug>
/**
* #brief Uploader::Uploader
* #param parent
*/
Uploader::Uploader(QObject *parent) :
QObject(parent)
{
uploadInProgress = false;
}
/**
* #brief Uploader::upload
* #param absoluteFilePath
*/
void Uploader::upload(QString absoluteFilePath)
{
qDebug() << "Upload Starting";
QFileInfo fileInfo(absoluteFilePath);
QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
//action part
QHttpPart textPart;
textPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"cmd\""));
textPart.setBody(QString("wFile").toLatin1());
//File Path
QHttpPart filePathPart;
filePathPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"file_path\""));
filePathPart.setBody(absoluteFilePath.toLatin1());
//filepart
QHttpPart filePart;
QMimeDatabase db;
QMimeType mime = db.mimeTypeForFile(absoluteFilePath);
filePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant(mime.name()));
filePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"preview_file\"; filename=\""+ fileInfo.baseName() + "\""));
QFile *file = new QFile(absoluteFilePath);
if ( !file->exists() )
{
qDebug() << "File Does not exist";
}
file->open(QIODevice::ReadOnly);
filePart.setBodyDevice(file);
file->setParent(multiPart); // we cannot delete the file now, so delete it with the multiPart
multiPart->append(textPart);
multiPart->append(filePathPart);
multiPart->append(filePart);
QUrl url("http://encryptor.dbz.dev/index.php?controller=wapi&action=handle");
QNetworkRequest request(url);
pManager = new QNetworkAccessManager();
pReply = pManager->post(request, multiPart);
multiPart->setParent(pReply);
pELoop = new QEventLoop();
connect(pReply, SIGNAL(uploadProgress(qint64,qint64)),this,SLOT(uploadProgress(qint64,qint64)));
connect(pReply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(onError(QNetworkReply::NetworkError)));
connect(pReply, SIGNAL(finished()),this, SLOT(uploadFinished()));
pELoop->exec();
// here connect signals etc.
uploadInProgress = true;
}
/**
* #brief Uploader::uploadFinished
*/
void Uploader::uploadFinished()
{
QString data = (QString) pReply->readAll();
qDebug() << data;
qDebug() << "Upload finished";
uploadInProgress = false;
if ( pReply->error() > 0 )
{
qDebug() << "Error occured: " << pReply->error() << " : " << pReply->errorString();
}
else
{
qDebug() << "Upload success";
}
pReply->deleteLater();
pELoop->exit();
}
void Uploader::uploadProgress(qint64 a, qint64 b)
{
qDebug() << " SOME PROGRESS!";
qDebug() << a << "/" << b;
}
void Uploader::onError(QNetworkReply::NetworkError err)
{
qDebug() << " SOME ERROR!";
qDebug() << err;
}
The request is executed as expected, and the signals are working as well.
I get the output of:
Upload Starting
SOME PROGRESS!
16384 / 483753
SOME PROGRESS!
483753 / 483753
SOME PROGRESS!
0 / 0
"Array
(
[controller] => wapi
[action] => handle
[cmd] => wFile
[file_path] => D:/Downloads/putty.exe
)
{"cmd":"","status":"","message":"","params":[]}"
Upload finished
Upload success
I leave this hear in case somebody is looking for a working example.
Related
In the example below sometimes the file is downloaded correctly, and sometimes, I'm getting these values in the qDebug() added into the downloadProgress lambda:
Percent complete: -1.08905e+09
Downloaded 11623330 of -1
And then the download fails, I mean it saves a zip file with 0 bytes.
QNetworkAccessManager *manager = new QNetworkAccessManager(this);
QNetworkRequest request;
// Random link just to test:
request.setUrl(
QUrl("https://github.com/FFmpeg/FFmpeg/archive/refs/tags/n5.0.2.zip"));
QNetworkReply *reply = manager->get(request);
connect(reply, &QNetworkReply::downloadProgress,
[this, reply](qint64 bytesReceived, qint64 bytesTotal)
{
qDebug() << "Downloaded " << bytesReceived << " of " << bytesTotal;
double percentComplete = (bytesReceived * 100.0) / bytesTotal;
qDebug() << "Percent complete: " << percentComplete;
});
connect(reply, &QNetworkReply::finished, [this, reply]()
{
if (reply->error() != QNetworkReply::NoError)
{
qDebug() << "Error: " << reply->errorString();
} else
{
QString fileName = "C:/Users/Raja/Downloads/file.zip";
QFile file(fileName);
if (file.open(QIODevice::WriteOnly))
{
file.write(reply->readAll());
file.close();
qDebug() << "File downloaded successfully";
} else
qDebug() << "Error: Unable to open the file";
}
reply->deleteLater();
});
What i'm missing?
Did you read the documentation?
It says that bytesTotal is -1 if the total size is unknown and
The download is finished when bytesReceived is equal to bytesTotal. At that time, bytesTotal will not be -1.
In other words: That behavior is expected and just means the download is still in progress. This probably happens when the server doesn't send a content-length header. See What's the "Content-Length" field in HTTP header?
I'm having trouble sending data from a server to a client using Qt. Whenever I do QTcpSocket::waitForBytesWritten() after a call to QTcpSocket::write(...), it returns false.
I tried using the bytesWritten signal, but that never gets emitted, presumably because no data is able to be written, and no data is received on the client side.
The writeData method is what is being called in the MainWindow class, but to try and narrow down the cause of the problem, I moved the writing of data to the client into the newConnection method.
The message Connection received is printed to the output window. I'm sending the string Some random data in the newConnection method to the client for testing purposes, but this is not being received by the client (the code to output the received data on the client side is inside Character::readData() method).
The value of the returnValue variable is true, and the code returns from the call to the client->waitForBytesWritten(-1) method. client->errorString() gives Unknown error, and then the message Bytes written is printed (even though, evidently, nothing is written, but I'm just using it as a status message).
Server.cpp
#include "Server.h"
Server::Server(QObject *parent) : QObject(parent)
{
server = new QTcpServer(this);
qDebug() << connect(server, SIGNAL(newConnection()), SLOT(newConnection()));
qDebug() << connect(server, SIGNAL(bytesWritten()), SLOT(bytesWritten()));
qDebug() << "Listening:" << server->listen(QHostAddress::Any, 1451);
server->waitForNewConnection(-1);
}
void Server::newConnection()
{
qDebug("Connection received");
client = server->nextPendingConnection();
client->write("Some random data\n");
bool returnValue = client->flush();
qDebug() << "Return value: " << returnValue;
qDebug() << client->waitForBytesWritten(-1);
qDebug() << "Error: " << client->errorString();
qDebug() << "Bytes written";
}
void Server::bytesWritten(qint64 bytes)
{
qDebug() << "Bytes written: " << QString::number(bytes);
}
void Server::writeData(std::string data)
{
QByteArray byteArray = QByteArray(data.c_str());
qDebug() << "Write data: " << QString::fromStdString(data);
client->write(byteArray);
}
Client.cpp
#include "Client.h"
#include "mainwindow.h"
Client::Client(QObject* parent) : QObject(parent)
{
socket = new QTcpSocket(this);
(void)QObject::connect(socket, SIGNAL(connected()), this, SLOT(connected()));
qDebug() << "Connect signal" << QObject::connect(socket, SIGNAL(readyRead()), this, SLOT(readData()));
}
bool Client::connectToHost(QString host)
{
socket->connectToHost(host, 1451);
socket->waitForConnected();
qDebug() << "Error: " << QString::number(socket->error() == QAbstractSocket::UnknownSocketError);
return true;
}
void Client::connected()
{
qDebug("Socket is connected");
qDebug() << QString::number(socket->state() == QAbstractSocket::ConnectedState);
}
void Client::readData()
{
qDebug("Read data");
QTcpSocket* sender = static_cast<QTcpSocket*>(QObject::sender());
QByteArray data = sender->readAll();
std::string character = data.toStdString();
qDebug() << "Character received: " << QString::fromStdString(character);
MainWindow::characterReceived(character);
}
I would have to send Modbus request for specific data, my problem is that I have to use mobile communication, wifi, connect to a custom electronic card, which is right in Modbus RTU.
My working code connecting to the electronic board:
#include "connessione.h"
#include <QModbusTcpClient>
#include <QVariant>
#include <QModbusDataUnit>
#include <QDebug>
connessione::connessione(QObject *parent) : QObject(parent)
{
qDebug() << "here debug " << "ok";
clientX = new QModbusTcpClient();
clientX->setConnectionParameter(QModbusDevice::NetworkAddressParameter, "192.168.222.1");
clientX->setConnectionParameter(QModbusDevice::NetworkPortParameter, 5555);
if (clientX->connectDevice())
{
qDebug() << "connected: " << clientX->state();
}
else
{
qDebug() << "ERRORE" << clientX->errorString();
}
}
void connessione::clickButton(){
QModbusDataUnit readUnit(QModbusDataUnit::HoldingRegisters, 0, 1); // just read input register 40006
//qDebug() << "readUnit" << readUnit.RegisterType;
qDebug() << "readUnit" << clientX->state();
if (auto *reply = clientX->sendReadRequest(readUnit, 255)) // client id 255
{
if (!reply->isFinished())
{
// connect the finished signal of the request to your read slot
qDebug() << "connected" << reply->errorString();
connect(reply, &QModbusReply::finished, this, &connessione::readReady);
}
else
{
qDebug() << "Errore" << reply->errorString();
delete reply; // broadcast replies return immediately
}
}
else
{
qDebug() << "Errore" << reply->errorString();
// request error
}
}
void connessione::readReady()
{
QModbusReply *reply = qobject_cast<QModbusReply *>(sender());
if (!reply)
return;
if (reply->error() == QModbusDevice::NoError)
{
const QModbusDataUnit unit = reply->result();
int startAddress = unit.startAddress(); // the start address,
int value = unit.value(0); // value of the start address + 0
qDebug() << "NESSUN ERRORE" << reply->errorString();
}
else
{
qDebug() << "Errore readReady" << reply->errorString();
// reply error
}
reply->deleteLater(); // delete the reply
}
log string TCP sent:
D/libmodbusMobile.so( 8042): (null):0 ((null)): qt.modbus: (TCP
client) Sent TCP PDU: 0x0300000001 with tId: 2
this is right: 0x0300000001
But unfortunately, my electronic card, the integrated firmware I can not modify, is right with Modbus RTU, so I should change 0x0300000001 to 0x010300000001C1C2 where C1 and C2 are the checksums.
I believe that QModbusDataUnit generate buffer to send. So how to change it? Exist manual solution where I build the buffer?
how to change it and create custom send buffer like the example?
Thanks
I have a QWebView where I'm watching the network requests by connecting:
QObject::connect(page()->networkAccessManager(),
SIGNAL(finished(QNetworkReply*)),
this,
SLOT(networkLoaded(QNetworkReply*)));
then:
void browserControl::networkLoaded(QNetworkReply *reply)
{
const QUrl reqUrl = reply->request().url();
qDebug() << "url = " << reqUrl;
QByteArray array = reply->readAll();
QString data = QString::fromUtf8(array.data(), array.size());
int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).value<int>();
qDebug() << "data = " << data;
qDebug() << "http code = " << statusCode;
}
But data is always empty, not matter if statusCode is 200. browserControl class is inheried from QWebView class.
You get no data because QWebPage read all data before your slot is called
So I want to upload a file using this code in qt:
QFile *file = new QFile(this->itemsToUpload.at(index));
QUrl url("http://leonardogalli.ch/beta/upload_single.php");
QNetworkRequest request(url);
QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
QHttpPart loginPart;
/* password */
loginPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"password\""));
loginPart.setBody("pass");
multiPart->append(loginPart);
loginPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"path\""));
loginPart.setBody(this->paths.at(index).toLocal8Bit());
qDebug() << this->paths.at(index).toLocal8Bit();
multiPart->append(loginPart);
QHttpPart filePart;
/* important that the files[] variable have the brackets, for PHP to interpret correctly */
filePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"uploaded\"; filename=\""+ file->fileName() + "\""));
qDebug() << "form-data; name=\"file1\"; filename=\""+ file->fileName() + "\"";
file->open(QIODevice::ReadOnly);
//qDebug() << file->readAll();
filePart.setBodyDevice(file);
file->setParent(multiPart); // we cannot delete the file now, so delete it with the multiPart
multiPart->append(filePart);
QNetworkReply* reply = networkManager->post(request, multiPart);
this->currentReply = reply;
multiPart->setParent(reply); // delete the multiPart with the reply
QObject::connect(reply,SIGNAL(uploadProgress(qint64,qint64)), this, SLOT(uploadProgress(qint64,qint64)));
QObject::connect(reply, SIGNAL(finished()), this, SLOT(finished()));
QObject::connect(reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(downloadProgress(qint64,qint64)));
}
void UploadManager::uploadProgress(qint64 bytesSent, qint64 bytesTotal)
{
if(bytesSent!=0 && bytesTotal != 0)
{
qDebug() << "Uploaded:" << bytesSent << " bytes of total: " << bytesTotal << "diff to total: " << bytesTotal-this->totalSize;
qint64 bytesDiff = bytesSent - this->lastUlSize;
this->UlSize += bytesDiff;
this->lastUlSize = bytesSent;
int timeDiff = QDateTime::currentMSecsSinceEpoch()/1000-this->time;
if(timeDiff!= 0 && bytesSent!=0 && bytesTotal != 0)
{
qDebug() << "Uploadspeed: " << ((this->UlSize/timeDiff));
qint64 remainingUl = this->totalSize - this->UlSize;
qint64 speed = ((this->UlSize/timeDiff)); //Download Speed in B/s
int remainingTimeSec = remainingUl/speed;
qDebug() << "Remaining time: " << this->readableTime(remainingTimeSec) << " percentage: " << (this->UlSize/this->totalSize)*100;
float percentage = ((float)this->UlSize/(float)this->totalSize)*100;
emit uploadProg(this->niceSpeed(speed), this->readableTime(remainingTimeSec), percentage);
}
}
}
The problem is there is a really long delay between the last uploadProgress signal and the finished signal. According to this question this is due to the uploadProgress not being the progress when a chunk of data has finished uploading. So how would I get the real uploadProgress, when a chunk is finished uploading?
Update 1:
On mac it seems to work perfectly, so why wont it work on windows?