Downloading file from Url



  • 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



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



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


  • Moderators

    [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.



  • [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.



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



  • [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;
    @


  • Moderators

    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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.