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
dbigham
Posts: 515
Registered: ‎04-01-2009
My Device: Z10, PlayBook
My Carrier: NA
Accepted Solution

QNetworkReply: uploadProgress finishes almost immediately

The QNetworkReply class allows the uploadProgress signal to be captured. I'm doing this when uploading a file via POST.  I then update a progress bar.  Unfortunately, the progress bar zooms across in about 0.3 seconds, while the actual upload takes 6 seconds. (I'm uploading 150 KB on a 0.2 Mbps upload connection)

 

I did some Google searching and found other people complaining about this, but no hint of why, or how to fix this.

Please use plain text.
Developer
pyth
Posts: 501
Registered: ‎01-19-2011
My Device: My Trusty Red Plane
My Carrier: Outer Space

Re: QNetworkReply: uploadProgress finishes almost immediately

can you post your code? maybe you made some mistake in calculating the percentage (dividing int by int)

-----------------------------------------------------------------------
I'm a bird from outer space. But I'm not flappy o.o
Please use plain text.
Developer
NicoBB
Posts: 31
Registered: ‎05-31-2013
My Device: Z10, Q10

Re: QNetworkReply: uploadProgress finishes almost immediately

[ Edited ]

Please ignore my previous comment. I was thinking you are listening for the downloadProgress.

 

If you use bytesSend and bytesTotal correctly as in https://developer.blackberry.com/native/reference/cascades/qnetworkreply.html#uploadProgress and you still get the problem, but the file is reaching the destination, then you can consider that the data is buffered first and that would be the end of the mission for QNetworkReply. One easy solution would be to use an activity indicator instead of the progressindicator in the ui. The hard way would be to use one of the sockets, if you can handle them on the server.

Hope it helps.

Any other ideas?

If you find an answer helpful hit Like. If your problem was solved hit Accept as Solution.

My Apps: SanQuiz
Please use plain text.
Developer
dbigham
Posts: 515
Registered: ‎04-01-2009
My Device: Z10, PlayBook
My Carrier: NA

Re: QNetworkReply: uploadProgress finishes almost immediately

That was a good thought pyth, and I did actually have that bug in my code. (using integers)  But that of course wouldn't explain it because while below 50%, the progress bar should be 0, and after 50% it should be 100%.  (and it would look like a smooth transition due to Cascades implicit animations)

 

Here's the code:

 

void PhotosModel::uploadToFace(PhotoShare photo)
{
    QString photoFilename = photo.getFilename();
    QFile photoFile(photoFilename);

    QString urlStr = "http://apius.faceplusplus.com/detection/detect";

    QList<QPair<QString, QString> > queryParameters;

    QString key = Utility::getFaceApiKey();
    QString secret = Utility::getFaceApiSecret();

    queryParameters.append(QPair<QString, QString>("api_key", key));
    queryParameters.append(QPair<QString, QString>("api_secret", secret));

    QUrl url = QUrl(urlStr);

    url.setQueryItems(queryParameters);

    QString resizedPhotoFilename = preparePhotoForUpload(photo.getFilename());

    QNetworkRequest request(url);

    QString bound = "83f11d350617544d8d7765917f60b01f19fc9487cc0221e1048e4b3c00e4";
    QByteArray data(QString("--" + bound + "\r\n").toAscii());
    data.append("Content-Disposition: form-data; name=\"img\"; filename=\"tmp.jpg\"\r\n");
    data.append("Content-Type: application/octet-stream\r\n\r\n");
    QFile file(resizedPhotoFilename);
    if (!file.open(QIODevice::ReadOnly))
    {
        ErrorHelpers::showDialog("Error", "uploading a photo to Face++", QString("Couldn't open file: ") + resizedPhotoFilename, TW9);
        uploadFailedGoToNextPhoto();
        return;
    }
    data.append(file.readAll());
    data.append("\r\n");
    data.append("--" + bound + "--\r\n");

    qDebug() << "Data size: " << data.length();

    request.setRawHeader(QString("Content-Type").toAscii(), QString("multipart/form-data; boundary=" + bound).toAscii());
    request.setRawHeader(QString("Content-Length").toAscii(), QString::number(data.length()).toAscii());

    uploadReply = am->post(request, data);

    activityIndicator->setRunning(true);
    activityIndicator->setVisible(true);

    progressBar->setVisible(true);

    qtime.start();

    connect(uploadReply, SIGNAL(finished()), this, SLOT(faceUploadHandleResponse()));
    connect(uploadReply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(uploadError(QNetworkReply::NetworkError)));
    connect(uploadReply, SIGNAL(uploadProgress(qint64,qint64)), this, SLOT(faceDetectionUploadProgress(qint64,qint64)));
}

void PhotosModel::faceDetectionUploadProgress(qint64 bytesSent, qint64 bytesTotal)
{
    qDebug() << qtime.elapsed() << " ms: " << bytesSent << " / " << bytesTotal << " (" << ((float)bytesSent / (float)bytesTotal * 100) << "% )";

    progressBar->setValue((float)bytesSent / (float)bytesTotal);
}

 

Notice that I have added a QTimer (qtime), which gets printed out upon  faceDetectionUploadProgress events. Here's the output of that:

 

530 ms: 16384 / 158249 ( 10.3533 % )
532 ms: 32768 / 158249 ( 20.7066 % )
533 ms: 49152 / 158249 ( 31.0599 % )
535 ms: 65536 / 158249 ( 41.4132 % )
536 ms: 81920 / 158249 ( 51.7665 % )
537 ms: 98304 / 158249 ( 62.1198 % )
539 ms: 114688 / 158249 ( 72.4731 % )
540 ms: 131072 / 158249 ( 82.8264 % )
540 ms: 147456 / 158249 ( 93.1797 % )
540 ms: 158249 / 158249 ( 100 % )

 

Notice that it would appear that the faceDetectionUploadProgress slot is getting called every millisecond or so, and each time, progress increases by about 10%, which is impossible with my internet connection.

 

So as was otherwise suggested, it seems like there is some layer of buffering between my app and the actual sending of the data.  It's kind of maddening, because you'd expect such a simple mechanism as upload progress to work as expected.  Isn't it false advertising if Qt adds such a method but it doesn't even work properly?

 

I have one other random crazy thought... I wonder if this is somehow related to the use of a simulator?  Is it possible that adds some kind of weird buffering layer?  I suppose I should try on the actual device...

Please use plain text.
Developer
dbigham
Posts: 515
Registered: ‎04-01-2009
My Device: Z10, PlayBook
My Carrier: NA

Re: QNetworkReply: uploadProgress finishes almost immediately

Wow, it would in fact appear to be a simulator issue. When run on the device, I get:

 

3685 ms: 16384 / 321796 ( 5.09142 % )
3687 ms: 32768 / 321796 ( 10.1828 % )
3689 ms: 49152 / 321796 ( 15.2743 % )
3689 ms: 65536 / 321796 ( 20.3657 % )
3690 ms: 81920 / 321796 ( 25.4571 % )
3692 ms: 98304 / 321796 ( 30.5485 %)
3701 ms: 114688 / 321796 ( 35.64 % )
3701 ms: 131072 / 321796 ( 40.7314 % )
3703 ms: 147456 / 321796 ( 45.8228 % )
3704 ms: 163840 / 321796 ( 50.9142 % )
3705 ms: 180224 / 321796 ( 56.0057 % )
18930 ms: 196608 / 321796 ( 61.0971 % )
18932 ms: 212992 / 321796 ( 66.1885 % )
22694 ms: 229376 / 321796 ( 71.2799 % )
22695 ms: 245760 / 321796 ( 76.3714 % )
26180 ms: 262144 / 321796 ( 81.4628 % )
26181 ms: 278528 / 321796 ( 86.5542 % )
30280 ms: 294912 / 321796 ( 91.6456 % )
30282 ms: 311296 / 321796 ( 96.7371 % )
35082 ms: 321796 / 321796 ( 100 % )

Please use plain text.