1

私は画像のアップロードのために次のコードを試しましたが、このコードではサーバーから応答を取得できますが、サーバーでは画像サイズは常にゼロです。この点について誰かが私を助けてくれますか?

  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";
    }
}
4

1 に答える 1

1
QString body = "" ;

const QUrl url("http://someurl.to/make/the/post" );
 QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
 QHttpPart textPartData;
 body = "" ;
 QTextStream(&body) << "";
 textPartData.setHeader(QNetworkRequest::ContentTypeHeader, "application/json; charset=utf-8");
 textPartData.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"data\""));
 textPartData.setBody(body.toAscii());

     for(int i=0; i< number_of_files_to_send ; i++){
             QHttpPart imagePart;
             QString header_body = "";
             QTextStream(&header_body) << "form-data; name=\"" << name_of_the_file << "\"; filename=\"" << name_of_the_file << ".jpg\"" ;
             imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant(header_body));
             imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/jpeg"));

             //prepare file for upload
             QString filepath;
             QTextStream(&filepath) << FunkyPigeonBlackberry::getInstance()->dataModelProductDetails()->custom_image(i).value("cut_image").toString();

             // pack the new file
             QFile *file = new QFile(filepath);
             file->open(QIODevice::ReadOnly);
             imagePart.setBodyDevice(file);
             file->setParent(multiPart); // we cannot delete the file now, so delete it with the multiPart
             multiPart->append(imagePart);
     }

 multiPart->append(textPartData);
 multiPart->append(textPartProductDetails);
 multiPart->append(textPartDataDelivery);


 QNetworkRequest request(url);

 QNetworkReply* reply = m_networkAccessManager->post(request, multiPart);
 connect(reply, SIGNAL(finished()), this, SLOT(onNetworkResponse()));
于 2013-05-07T06:45:46.760 に答える