Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Contributor
Posts: 11
Registered: ‎01-24-2013
My Device: BB10
My Carrier: Airtel

Uploading Image on Server is not working

Hi All,

 

I am getting some issue while Uploading Image on the server. I tried CODE from this link (https://developer.blackberry.com/cascades/reference/qhttpmultipart.html), but i am getting server response like XML file. This is my CODE :-

QHttpMultiPart *multiPart = new QHttpMultiPart(
            QHttpMultiPart::FormDataType);

    /*QHttpPart textPart;
     textPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"text\""));
     textPart.setBody("my text");*/

    QHttpPart imagePart;
    imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/jpeg"));
    imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"image\""));
    QFile *file = new QFile(qImagePath/*"image.jpg"*/);
    file->open(QIODevice::ReadOnly);
    imagePart.setBodyDevice(file);
    file->setParent(multiPart); // we cannot delete the file now, so delete it with the multiPart

    /* multiPart->append(textPart);*/
    multiPart->append(imagePart);

    QUrl url("http://dev.lobbyfriend.net/statuswithpic");
    QNetworkRequest request(url);

    //QNetworkAccessManager manager;
    QNetworkReply *reply = mNetAccessMngr->post(request, multiPart);
//    mNetAccessMngr->post(request,multiPart);
    multiPart->setParent(reply); // delete the multiPart with the reply
    // here connect signals etc.
    bool result = connect(mNetAccessMngr, SIGNAL(finished(QNetworkReply*)),
            this, SLOT(requestFinished(QNetworkReply*)));
    Q_ASSERT(result);
    Q_UNUSED(result);

Developer
Posts: 16
Registered: ‎12-09-2012
My Device: BlackBerry Z10
My Carrier: Umniah

Re: Uploading Image on Server is not working

Replace the following two lines:

   imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/jpeg"));
   imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"image\""));

 

with the following:

imagePart->setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"File\"; filename=\"image.jpg\""));
imagePart->setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/jpeg"));

 

it should work.

 

 

Forums Advisor I
Posts: 501
Registered: ‎03-01-2013
My Device: BlackBerry Z10

Re: Uploading Image on Server is not working

Hi

I am trying to upload the image to server using QHttpMultiPart and as suggested i have replaced the code with the above lines but still its not working.

Any suggestion????