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
Highlighted
Contributor
Posts: 29
Registered: ‎09-07-2012
My Device: 9900
My Carrier: Airtel

Upload the image to server in blackberry 10 cascades?

I have tried the following code for image uploading,in this code am able to get the response from server but the image size is always zero in server.Can anyone help me about this regards,

 

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

 

Developer
Posts: 68
Registered: ‎08-16-2012
My Device: BB10 Dev Alpha
My Carrier: NA

Re: Upload the image to server in blackberry 10 cascades?

I haven't tried the way you do, which you create a post request manually. But i have tried the multipart wich is easy and convient to use. Please check out this thread: http://supportforums.blackberry.com/t5/Cascades-Development/Upload-camera-image-Cascades-10-Beta-3/m...

Dong
Developer
Posts: 217
Registered: ‎09-18-2009
My Device: Z10
My Carrier: O2

Re: Upload the image to server in blackberry 10 cascades?

There is a good example on how to implement a post request.

 

    QFile fileUp("FILE PATH");
    fileUp.open(QIODevice::ReadOnly);
    QByteArray file(fileUp.readAll());
    fileUp.close();
     
    QByteArray boundary("AaB03x");
     
    QNetworkRequest req(QUrl("https://evsweb39.idrive.com/evs/uploadFile"));
    req.setRawHeader("Content-Type", QByteArray("multipart/form-data; charset=UTF-8; boundary=") + boundary);
     
    QByteArray postData;
    postData += "--" + boundary + "\r\n";
    postData += "Content-Disposition: form-data; name=\"uid\"\r\n\r\n";
    postData += userName + "\r\n";
     
    postData += "--" + boundary + "\r\n";
    postData += "Content-Disposition: form-data; name=\"pwd\"\r\n\r\n";
    postData += passWord + "\r\n";
     
    postData += "--" + boundary + "\r\n";
    postData += "Content-Disposition: form-data; name=\"p\"\r\n\r\n";
    postData += remotePath + "\r\n";
     
    postData += "--" + boundary + "\r\n";
    postData += "Content-Disposition: form-data; filename=\"" + fileName + "\";\r\nContent-Type: application/octet-stream\r\n\r\n";
    postData += file;
    postData += "\r\n";
     
    postData += "--" + boundary + "--\r\n";
     
    req.setHeader(QNetworkRequest::ContentLengthHeader, QString::number(postData.length()));

 

This example is part of another forum thread.

 

http://www.qtcentre.org/threads/48475-POST-Request-problems-with-IDrive-EVS-REST-APIs

Contributor
Posts: 29
Registered: ‎09-07-2012
My Device: 9900
My Carrier: Airtel

Re: Upload the image to server in blackberry 10 cascades?

okay thanks..Can you give me the php server side code also?

Contributor
Posts: 29
Registered: ‎09-07-2012
My Device: 9900
My Carrier: Airtel

Re: Upload the image to server in blackberry 10 cascades?

Finally i have tried the below link and it works fine..

http://www.qtforum.org/article/31597/problems-with-uploading-big-file.html