我已经尝试使用以下代码上传图像,在此代码中能够从服务器获得响应,但服务器中的图像大小始终为零。任何人都可以帮助我解决这方面的问题,
void App::uploadImage(){
QString path(QDir::currentPath() + "/app/native/assets/Icon/img.jpg");
QNetworkRequest request(
QUrl( "http://domain.com?type=uploadimages&deviceid=12323"));
QByteArray boundary = "-------------------------32142544626177";
QFile file(path);
if (!file.open(QIODevice::ReadOnly)) {
qDebug() << "error read image";
return;
}
QByteArray fileContent(file.readAll());
QByteArray data = "--" + boundary + "\r\n";
data +=
"Content-Disposition: form-data; name=\"data\"; filename=\"img.jpg\";\r\n";
data += "Content-Type: image/jpg\r\n\r\n" + fileContent + "\r\n";
data += "--" + boundary + "--\r\n";
request.setRawHeader("Content-Type",
"multipart/form-data; boundary=" + boundary);
request.setRawHeader("Content-Length",
QString::number(data.size()).toAscii());
file.close();
qDebug() << "data" << data.size();
QNetworkAccessManager *am = new QNetworkAccessManager(this);
QNetworkReply *reply = am->post(request, "&data=" +data);
QObject::connect(am, SIGNAL(finished(QNetworkReply*)), this,
SLOT(replyFinished(QNetworkReply*)));
}
void App::replyFinished(QNetworkReply* reply) {
reply->open(QIODevice::ReadOnly);
if (reply->error() == QNetworkReply::NoError) {
QByteArray str = (reply->readAll());
QString response = QString::fromUtf8(str.data(), str.size());
qDebug() << " response " << response;
}
else{
qDebug() << "error response server";
}
}