在QT中获得真正的上传进度

时间:2014-12-24 16:04:57

标签: c++ qt file-upload progress

所以我想在qt:

中使用此代码上传文件
    QFile *file =  new QFile(this->itemsToUpload.at(index));
    QUrl url("http://leonardogalli.ch/beta/upload_single.php");
    QNetworkRequest request(url);

    QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
    QHttpPart loginPart;
    /* password */
    loginPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"password\""));
    loginPart.setBody("pass");
    multiPart->append(loginPart);
    loginPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"path\""));
    loginPart.setBody(this->paths.at(index).toLocal8Bit());
    qDebug() << this->paths.at(index).toLocal8Bit();
    multiPart->append(loginPart);
    QHttpPart filePart;
    /* important that the files[] variable have the brackets, for PHP to interpret correctly */
    filePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"uploaded\"; filename=\""+ file->fileName() + "\""));
    qDebug() << "form-data; name=\"file1\"; filename=\""+ file->fileName() + "\"";
    file->open(QIODevice::ReadOnly);
    //qDebug() << file->readAll();
    filePart.setBodyDevice(file);
    file->setParent(multiPart); // we cannot delete the file now, so delete it with the multiPart
    multiPart->append(filePart);



    QNetworkReply* reply = networkManager->post(request, multiPart);
    this->currentReply = reply;
    multiPart->setParent(reply); // delete the multiPart with the reply
    QObject::connect(reply,SIGNAL(uploadProgress(qint64,qint64)), this, SLOT(uploadProgress(qint64,qint64)));
    QObject::connect(reply, SIGNAL(finished()), this, SLOT(finished()));
    QObject::connect(reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(downloadProgress(qint64,qint64)));
}
void UploadManager::uploadProgress(qint64 bytesSent, qint64 bytesTotal)
{
    if(bytesSent!=0 && bytesTotal != 0)
    {
    qDebug() << "Uploaded:" << bytesSent << " bytes of total: " << bytesTotal << "diff to total: " << bytesTotal-this->totalSize;
    qint64 bytesDiff = bytesSent - this->lastUlSize;
    this->UlSize += bytesDiff;
    this->lastUlSize = bytesSent;
    int timeDiff = QDateTime::currentMSecsSinceEpoch()/1000-this->time;
    if(timeDiff!= 0 && bytesSent!=0 && bytesTotal != 0)
    {
        qDebug() << "Uploadspeed: " << ((this->UlSize/timeDiff));

        qint64 remainingUl = this->totalSize - this->UlSize;
        qint64 speed = ((this->UlSize/timeDiff)); //Download Speed in B/s
        int remainingTimeSec = remainingUl/speed;
        qDebug() << "Remaining time: " <<  this->readableTime(remainingTimeSec) << " percentage: " << (this->UlSize/this->totalSize)*100;
        float percentage = ((float)this->UlSize/(float)this->totalSize)*100;
        emit uploadProg(this->niceSpeed(speed), this->readableTime(remainingTimeSec), percentage);
    }
    }
}

问题是最后一次uploadProgress信号与完成信号之间存在很长的延迟。根据{{​​3}}问题,这是因为当一大块数据完成上传时,uploadProgress不是进度。那么当一个块完成上传后,我将如何获得真正的uploadProgress?

更新1:

在Mac上看起来效果很好,所以为什么它不适用于Windows?

0 个答案:

没有答案