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
Developer
robert30
Posts: 191
Registered: ‎04-02-2013
My Device: BlackBerry Bold 9900
My Carrier: developer

Problem in image uploading to server

[ Edited ]

Hi all

I am getting problem in image uploading to server through http post method.

Here is my code:

 

main.qml


import bb.cascades 1.0

Page {

content: Container
{

horizontalAlignment: HorizontalAlignment.Fill
verticalAlignment: VerticalAlignment.Top

background: Color.Black

 

Container {

Button {

text: qsTr("Post!")

onClicked: {
_httpsample.post();


}


}
}

}

application.cpp

 

 

void ApplicationUI::smileytongue:ost()
{

QString bound;
QString crlf;
QString data;
QByteArray dataToSend;
QFile file("uploadedfile");
file.open(QIODevice::ReadOnly);

bound = "---------------------------7d935033608e2";
crlf = 0x0d;
crlf += 0x0a;
data = "--" + bound + crlf + "Content-Disposition: form-data; name=\"uploadedfile\"; ";
data += "filename=\"image_button.png\"";
data += crlf + "Content-Type: application/octet-stream" + crlf + crlf;
dataToSend.insert(0,data);
dataToSend.append(file.readAll());

dataToSend.append(crlf + "--" + bound + "--" + crlf);

QUrl url("url");
QNetworkRequest req(url);
req.setHeader(QNetworkRequest::ContentTypeHeader, tr("multipart/form-data; boundary=") + bound);
file.close();

QNetworkAccessManager * manager = new QNetworkAccessManager();

QNetworkReply * reply = manager->post(req, dataToSend);
connect(reply, SIGNAL(finished()), this, SLOT(onGetReply()));

 

}

 

 


void ApplicationUI::smileysurprised:nGetReply()
{
QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());

QString response;
if (reply) {
if (reply->error() == QNetworkReply::NoError) {
const int available = reply->bytesAvailable();
if (available > 0) {
const QByteArray buffer(reply->readAll());
response = QString::fromUtf8(buffer);
}
} else {
response = tr("Error: %1 status: %2").arg(reply->errorString(), reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toString());
qDebug() << response;
}

reply->deleteLater();
}

if (response.trimmed().isEmpty()) {
response = tr("Unable to retrieve post response");
}

emit complete(response);

 

 

}

 

Please help me

Please use plain text.
Developer
simon_hain
Posts: 16,113
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Problem in image uploading to server

and what would be the problem you are getting?

did you copy the code from somewhere specific?
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Developer
robert30
Posts: 191
Registered: ‎04-02-2013
My Device: BlackBerry Bold 9900
My Carrier: developer

Re: Problem in image uploading to server

Yes i have copied the code.

My problem is when i click the button i am not getting any output.

Please use plain text.
Developer
simon_hain
Posts: 16,113
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Problem in image uploading to server

add debug output and check how far the code works.
check if the sample code you use works on its own, then find out what you modified that broke it.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Developer
robert30
Posts: 191
Registered: ‎04-02-2013
My Device: BlackBerry Bold 9900
My Carrier: developer

Re: Problem in image uploading to server

hi, i have used the post method of http sample in my own code. in that sample when we click on the post button the text we enter get posted to that server and we get a response on new page but now in my app i have deleted the text area and when i click on the post button the responce will be displayed on that particular page only. But the problem is that when i click on the button nothing happens

*PS: i have mentioned the code above.

Please use plain text.