参考网址:
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();//取消下载