How to move the downloaded file to specified directory in Qt
-
just as title ,here is my code ,but I can't specified directory of the downloaded file.
DownLoadFile.cpp
@
CDownLoadFile::CDownLoadFile(QObject *parent)
:QObject(parent)
{}
void CDownLoadFile::downloadFile(QUrl url)
{
QFileInfo fileInfo(url.path());
QString fileName = fileInfo.fileName();
pfile = new QFile(fileName);
pfile->open(QIODevice::WriteOnly);
pmanager=new QNetworkAccessManager();
preply= pmanager->get(QNetworkRequest(url));
QObject::connect(preply, SIGNAL(readyRead()),this, SLOT(httpReadyRead()));
QObject::connect(preply,SIGNAL(finished()),this,SLOT(httpFinished()));
}void CDownLoadFile::httpReadyRead()
{
if (pfile)
pfile->write(preply->readAll());
}void CDownLoadFile::httpFinished()
{
QDir directory(QDir::current());
directory.mkdir(QString("tmpFile"));
directory.cd("./tmpFile");
pfile->flush();
pfile->close();
preply->deleteLater();
preply = 0;
delete pfile;
pfile = 0;
}@
The result is that the downloaded file is in the application's current direct ,not in the subdirectory of app ,that is tmpFile directory!
Thank you very much!
My best regards! -
Easiest would be:
@void CDownLoadFile::downloadFile(QUrl url, const QString &fileName)
{
pfile = new QFile(fileName);
pfile->open(QIODevice::WriteOnly);
pmanager=new QNetworkAccessManager();
preply= pmanager->get(QNetworkRequest(url));
QObject::connect(preply, SIGNAL(readyRead()),this, SLOT(httpReadyRead()));
QObject::connect(preply,SIGNAL(finished()),this,SLOT(httpFinished()));
}@That means you open the file in its target location immediately and saves you the copying. If you use QFileDialog to let the user find the file, you get the existing file checks for free.
-
thank you for your advice,It works ! Thanks again!