2013-01-08 65 views
1

我需要上傳圖像到我已經使用文件選擇器來獲取圖像的服務器,我重視我的樣本代碼,但它不工作上傳圖像到服務器黑莓10 QML

我的文件選擇器代碼

在C
     ` Button { 
        text: "Change Profile Image" 
        onClicked { 
         filePicker.open(); 
        }     
         attachedObjects: [ 
         FilePicker { 
          id: filePicker 
          property string selectedFile 
          type: FileType.Picture 
          onFileSelected: { 
           selectedFile = selectedFiles[0] 
           image.imageSource = selectedFile 
            _settings.showToast(selectedFile); 
            _settings.isUpdate(selectedFile); 
          } 
         } 
        ]` 

我的圖像更新代碼++

   `void Settings::isUpdate(QString selectedFile){ 


    QString path("asset:///Icon/mess.jpg"); 

    //php script that receives the image 

    QNetworkRequest requete(QUrl("http://192.168.1.176:2222/api?type=uploadimages&name=IMG_00000001.jpg&pin=123456")); //our server with php-script 

    QByteArray boundary = "-------------------------87142694621188"; 
    QFile file(path); 
    if (!file.open(QIODevice::ReadOnly)) 
    { 
     qDebug()<<"erreur read image"; 
     return; 
    } 
    QByteArray fileContent(file.readAll()); 
    QByteArray data = "--" + boundary + "\r\n"; 
    data += "Content-Disposition: form-data; name=\"data\"; filename=\"IMG_00000001.jpg\";\r\n"; 
    data += "Content-Type: image/jpg\r\n\r\n" + fileContent + "\r\n"; 
    data += "--" + boundary + "--\r\n"; 

    requete.setRawHeader("Content-Type", "multipart/form-data; boundary=" + boundary); 
    requete.setRawHeader("Content-Length", QString::number(data.size()).toAscii()); 
    file.close(); 
    qDebug() <<"data"<< data.size(); 
    QNetworkAccessManager *am = new QNetworkAccessManager(this); 
    QNetworkReply *reply = am->post(requete,"&data="+data); 
    QObject::connect(am, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*))); 

}

 void Settings::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()<<" re "<<response; 
         } 

         else 
         qDebug()<<"error response server"; 
         }` 

我可以能夠得到響應,但不能將圖片上傳到服務器

感謝

+0

開始問一個問題:你問的幫助,但沒有告訴我們發生了什麼。順便說一句,你應該直接上傳一個QIODevice而不是QByteArray以獲得更好的內存性能 – Benoit

+1

我能夠從url獲得響應,但無法上傳圖像 – Vendetta

+0

嘗試直接執行am-> post一個QIODevice或byteArray。大小標題應自動生成 – Benoit

回答

0
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()));