文件下载问题

时间:2012-12-10 11:43:32

标签: c++ qt qnetworkaccessmanager qwebkit

我使用webkit有这个QT脚本。我可以下载文件没问题,但我无法在文件对话框中移动进度条。我认为在我调用进度对话框之前已经发送了网络回复,因为点击下载链接然后qDebug() << "Left click - download!";回显到控制台有延迟。如何在完成netwrok回复并调用unsupportedContent()方法之后拦截它?

修改 我可以删除它并使用reply = manager.get(QNetworkRequest(url));但我实际上并不知道它可能是用户点击的任何链接的URL,没有预定义的URL?

void MainWindow::unsupportedContent(QNetworkReply *reply) {

    qDebug() << "Left click - download!";
    qDebug() << "Bytes to download: " << reply->bytesAvailable();

    QString str = reply->rawHeader("Content-Disposition");

    QString end = str.mid(21);
    end.chop(1);

    qDebug() << "File name: " << end;
    qDebug() << "File type: " << reply->rawHeader("Content-Type");
    qDebug() << "File size (bytes): " << reply->bytesAvailable();
    QString defaultFileName = QFileInfo(end).fileName();
    QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"), defaultFileName);
    if (fileName.isEmpty()) return;

    file = new QFile(fileName);
    if(!file->open(QIODevice::WriteOnly))
    {
        QMessageBox::information(this, "Downloader",
            tr("Unable to save the file %1: %2.")
            .arg(fileName).arg(file->errorString()));
        delete file;
        file = NULL;
        return;
    }

    downloadRequestAborted = false;

    connect(reply, SIGNAL(finished()), this, SLOT(downloadFinished()));
        connect(reply, SIGNAL(readyRead()), this, SLOT(downloadReadyRead()));
        connect(reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(downloadProgress(qint64,qint64)));
        connect(progressDialog, SIGNAL(canceled()), this, SLOT(cancelDownload()));
        progressDialog->setLabelText(tr("Downloading %1...").arg(fileName));
        //downloadButton->setEnabled(false);
        progressDialog->exec();


    //QFile file(fileName);
    //file.open( QIODevice::WriteOnly );
    //file.write(reply->read(reply->bytesAvailable()));
    //file.close();
}

void MainWindow::downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
{
    qDebug() << bytesReceived << bytesTotal;
    if(downloadRequestAborted)
        return;
    progressDialog->setMaximum(bytesTotal);
    progressDialog->setValue(bytesReceived);
}

void MainWindow::downloadReadyRead()
{
    if(file)
        file->write(reply->read(reply->bytesAvailable()));
}

void MainWindow::downloadFinished()
{
    qDebug() << "Download finished!";
    if(downloadRequestAborted)
    {
        if(file)
        {
            file->close();
            file->remove();
            delete file;
            file = NULL;
        }
        reply->deleteLater();
        progressDialog->hide();
        //downloadButton->setEnabled(true);
        return;
    }

    downloadReadyRead();
    progressDialog->hide();
    //downloadButton->setEnabled(true);
    file->flush();
    file->close();

    if(reply->error())
    {
        //Download failed
        QMessageBox::information(this, "Download failed", tr("Failed: %1").arg(reply->errorString()));
    }

    reply->deleteLater();
    reply = NULL;
    delete file;
    file = NULL;
}

void MainWindow::cancelDownload()
{
    downloadRequestAborted = true;
    reply->abort();
    progressDialog->hide();
    //downloadButton->setEnabled(true);
}

1 个答案:

答案 0 :(得分:0)

上面的方法一直在工作,问题是它收到的字节太小,你根本无法告诉它已经下载了,一旦我试图下载一个更大的文件,下载的字节就被充分显示了:)< / p>

这是我最终得到的方法,它可以接收请求并将其保存为磁盘。

void MainWindow::unsupportedContent(QNetworkReply *reply) {

    QString str = reply->rawHeader("Content-Disposition");

    QString end = str.mid(21);
    end.chop(1);

    QString defaultFileName = QFileInfo(end).fileName();
    QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"), defaultFileName);
    if (fileName.isEmpty()) return;

    file = new QFile(fileName);
    if(!file->open(QIODevice::WriteOnly))
    {
        QMessageBox::information(this, "Downloader",
            tr("Unable to save the file %1: %2.")
            .arg(fileName).arg(file->errorString()));
        delete file;
        file = NULL;
        return;
    }

    downloadRequestAborted = false;
    if(!reply->isFinished()){
    connect(reply, SIGNAL(downloadProgress(qint64, qint64)), SLOT(downloadProgress(qint64, qint64)));

    connect(progressDialog, SIGNAL(canceled()), SLOT(cancelDownload()));
    progressDialog->setLabelText(tr("Downloading %1...").arg(fileName));
    progressDialog->exec();
    //return;
    }

    if(downloadRequestAborted)
    {
        if(file)
        {
            file->close();
            file->remove();
            delete file;
            file = NULL;
        }
        reply->abort();
        reply->deleteLater();
        progressDialog->hide();
        return;
    }

    file->write(reply->read(reply->bytesAvailable()));
    file->flush();
    file->close();
    file = NULL;

    if(file == NULL){
        isDownload = true;
        fileURL = fileName;
    systray->showMessage("CytoViewer v1.0", "Download finished - Click to open", QSystemTrayIcon::NoIcon, 10000);
    }
}