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. Downloading file from Url
Qt 6.11 is out! See what's new in the release blog

Downloading file from Url

Scheduled Pinned Locked Moved General and Desktop
8 Posts 4 Posters 18.0k Views 1 Watching
  • 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.
  • G Offline
    G Offline
    glararan
    wrote on last edited by
    #1

    Hi, i trying using from Qt example http a download file functions... without GUI. Just after opening program auto download from URL.

    My edited code:
    @#include <QtNetwork>
    #include <QtGui>
    #include <QDebug>

    #include "Utils.h"

    Download::Download()
    {

    }

    void Download::startRequest(QUrl url)
    {
    netReply = netManager.get(QNetworkRequest(url));

    connect(netReply, SIGNAL(finished()), this, SLOT(slot_downloadFinished()));
    connect(netReply, SIGNAL(readyRead()), this, SLOT(slot_downloadReady()));
    

    }

    void Download::downloadFile(const QUrl &Url, const QString &dir)
    {
    url = Url;

    QFileInfo fileInfo(url.path(&#41;);
    QString fileName = fileInfo.fileName();
    if(fileName.isEmpty())
        fileName = "unknown.file";
    
    if(!dir.isNull())
        fileName = dir + "//" + fileName;
    
    if(QFile::exists(fileName))
    {
        int i = 0;
        fileName += ".";
    
        while(QFile::exists(fileName + QString::number(i)))
            ++i;
    
        fileName += QString::number(i);
    }
    
    file = new QFile&#40;fileName&#41;;
    if(!file->open(QIODevice::WriteOnly&#41;)
    {
        delete file;
        file = 0;
        return;
    }
    
    httpRequest = false;
    startRequest(url);
    

    }

    // Download Slots
    void Download::slot_downloadFinished()
    {
    if(httpRequest)
    {
    if(file)
    {
    file->close();
    file->remove();
    delete file;
    file = 0;
    }

        netReply->deleteLater();
        return;
    }
    
    file->flush();
    file->close();
    
    QVariant redirTar = netReply->attribute(QNetworkRequest::RedirectionTargetAttribute);
    if(netReply->error())
        file->remove();
    else if(!redirTar.isNull())
    {
        qDebug() << "Using redirTar";
    
        QUrl Url2 = url.resolved(redirTar.toUrl());
        url = Url2;
        netReply->deleteLater();
        file->open(QIODevice::WriteOnly);
        file->resize(0);
        startRequest(url);
    
        return;
    }
    
    netReply->deleteLater();
    netReply = 0;
    delete file;
    file = 0;
    

    }

    void Download::slot_downloadReady()
    {
    if(file)
    file->write(netReply->readAll());
    }@

    It download file but is empty :( I dont know why. I try using "Run as Administrator" but without changes.

    I calling "downloadFile" for download.,

    OS: Win 7
    Ver: 4.8.1

    1 Reply Last reply
    0
    • L Offline
      L Offline
      leon.anavi
      wrote on last edited by
      #2

      A simple example how to "download a file for URL is available at our wiki":http://qt-project.org/wiki/Download_Data_from_URL.

      http://anavi.org/

      1 Reply Last reply
      0
      • G Offline
        G Offline
        glararan
        wrote on last edited by
        #3

        You posted download for images... :D i need for all files.

        1 Reply Last reply
        0
        • K Offline
          K Offline
          koahnig
          wrote on last edited by
          #4

          [quote author="glararan" date="1335697602"]You posted download for images... :D i need for all files.[/quote]
          Did you try to use for another file extension?
          See, if it works. If not, you may post the error messages and ask for advise.

          Vote the answer(s) that helped you to solve your issue(s)

          1 Reply Last reply
          0
          • L Offline
            L Offline
            leon.anavi
            wrote on last edited by
            #5

            [quote author="glararan" date="1335697602"]You posted download for images... :D i need for all files.[/quote]

            No, class FileDownloader works for all kind of files because the content is downloaded as QByteArray.

            http://anavi.org/

            1 Reply Last reply
            0
            • G Offline
              G Offline
              glararan
              wrote on last edited by
              #6

              And can you show me how it is for .txt file? I have no idea.

              1 Reply Last reply
              0
              • L Offline
                L Offline
                leon.anavi
                wrote on last edited by
                #7

                [quote author="glararan" date="1335706829"]And can you show me how it is for .txt file? I have no idea.[/quote]

                Just write the content of the QByteArray to a file. You should do something like this:
                @
                QFile file("myfile.txt");
                file.open(QIODevice::WriteOnly);
                QDataStream out(&file);
                out << yourByteArray;
                @

                http://anavi.org/

                1 Reply Last reply
                0
                • ? Offline
                  ? Offline
                  A Former User
                  wrote on last edited by
                  #8

                  Are you actually calling QCoreApplication::exec somewhere? You need an event loop to process the network code.

                  Without it you will create the file, set up everything and then just quit before Qt ever has a chance to receive/process data from the network by calling your slots.

                  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