zoukankan      html  css  js  c++  java
  • QWebView下载文件,QUrl中解析文件名

    参考网址:

    http://blog.csdn.net/cdnight/article/details/23658715

    http://www.tuicool.com/articles/AzeaUz

    http://m.blog.csdn.net/blog/carecool/17881039

    自己的代码:

    ui->webView->page()->setForwardUnsupportedContent(true);
    
    
    connect( ui->webView->page(),SIGNAL(unsupportedContent(QNetworkReply*)),
                 this,SLOT(unsupportedContent(QNetworkReply*)) );

    void MainWindow::unsupportedContent(QNetworkReply *reply)
    {
    
    
        //save to a dir
        QString strDesktop = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
        QString strDir = QFileDialog::getExistingDirectory(this, "另存为",
                                                        strDesktop,
                                                        QFileDialog::ShowDirsOnly
                                                        | QFileDialog::DontResolveSymlinks);
        if(strDir=="")
        {
            return;
        }
    
    
    
    
        QString fileName;
        fileName.clear();
      //url转码
        QUrl copy(reply->url());
        copy.setQuery(copy.query(QUrl::FullyDecoded), QUrl::TolerantMode);
        qDebug()<<copy.toString();
        QStringList urlList =  copy.toString().split('&');
        foreach (QString str, urlList)
        {
            if(str.contains("downloadName",Qt::CaseSensitive))
            {
                QStringList downList = str.split('=');
                fileName = downList.at(1);
                m_pFile = new QFile(strDir + "\" + fileName);
                m_pFile->open(QIODevice::WriteOnly|QIODevice::Truncate);
            }
        }
    
    
        QNetworkRequest request;
        request.setUrl(reply->url());
    
    
        m_pManager = new QNetworkAccessManager(this);
        m_pReply = m_pManager->get(request);
        connect(m_pManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));//下载结束
        connect(m_pReply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT( onDownloadProgress(qint64 ,qint64 )));//更新进度条
        connect(m_pReply, SIGNAL(readyRead()), this, SLOT(on_readyRead()));//收到数据就往文件里写
    }
    void replyFinished(QNetworkReply* _reply)
    {
    
    
        //无错误返回
        if(_reply->error() == QNetworkReply::NoError)
        {
        //关闭文件
        }
        else
        {
          //处理错误
        }
    }


    void on_readyRead()
    {
        m_pFile->write(m_pReply->readAll());//向文件写入数据
    }
    
    
     m_pReply->abort();//取消下载
     
  • 相关阅读:
    转载:稳定性,鲁棒性和非脆弱性的精辟解读
    BZOJ 2806: [Ctsc2012]Cheat(单调队列优化dp+后缀自动机)
    CF 235C. Cyclical Quest(后缀自动机)
    BZOJ 5137: [Usaco2017 Dec]Standing Out from the Herd(后缀自动机)
    2019/2/28 考试记录
    后缀自动机的应用
    CF 452E. Three strings(后缀数组+并查集)
    BZOJ 2281: [Sdoi2011]黑白棋(dp+博弈论)
    CF 39E. What Has Dirichlet Got to Do with That?(记忆化搜索+博弈论)
    LUOGU P4783 【模板】矩阵求逆(高斯消元)
  • 原文地址:https://www.cnblogs.com/zhangxuan/p/5073906.html
Copyright © 2011-2022 走看看