Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to move the downloaded file to specified directory in Qt
Forum Update on Monday, May 27th 2025

How to move the downloaded file to specified directory in Qt

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 3.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • X Offline
    X Offline
    xiazhouquan
    wrote on last edited by
    #1

    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!

    业精于勤荒于嬉,行成于思毁于随

    1 Reply Last reply
    0
    • F Offline
      F Offline
      Franzk
      wrote on last edited by
      #2

      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.

      "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • X Offline
        X Offline
        xiazhouquan
        wrote on last edited by
        #3

        thank you for your advice,It works ! Thanks again!

        业精于勤荒于嬉,行成于思毁于随

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved