c++qt4.8qfilequrl

Unable to store Downloaded file from url in QT 4.8


I am trying to download a xml file using url in QT 4.8. Here is my code for this:-

downloadurl::downloadurl(QWidget *parent, Qt::WFlags flags)
    : QMainWindow(parent, flags)
{
    ui.setupUi(this);

    QNetworkAccessManager GetFileDownloadNetworkManager;
    QNetworkReply *GetFileDownloadNetworkReply;

    QObject::connect(&GetFileDownloadNetworkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(FileDownloadReplyReceived(QNetworkReply*)));
    QObject::connect(&GetFileDownloadNetworkManager, SIGNAL((QNetworkReply*)), this, SLOT(FileDownloadReplyReceived(QNetworkReply*)));

    QUrl url(QString("MyLinkToDownloadFile"));  
    QNetworkRequest reqpn(url);
    reqpn.setRawHeader("Content-Type", "text/xml");
    GetFileDownloadNetworkReply = GetFileDownloadNetworkManager.get(reqpn);
}

downloadurl::~downloadurl()
{

}

void downloadurl::FileDownloadReplyReceived(QNetworkReply *Reply)
{
    if(Reply->error() == QNetworkReply::NoError)
    {
        QFile DownloadedFile("/FlashDisk/ServerTemp/12012.xml");
        if (!DownloadedFile.open(QIODevice::WriteOnly))
        {
            messageBox.critical(0,"Error","Can't open File");
            return;
        }
        DownloadedFile.write(Reply->readAll());
        DownloadedFile.close();
    }
    Reply->deleteLater();
}

My code enters in slot but I can't find created 12012.txt in /FlashDisk/ServerTemp/ . please Tell what exactly I am missing here.


Solution

  • Solve issue by myself. I have to add QIODevice::Text permission in QFile.open. Here is my final code:-

    downloadurl::downloadurl(QWidget *parent, Qt::WFlags flags)
        : QMainWindow(parent, flags)
    {
        ui.setupUi(this);
    
        QNetworkAccessManager GetFileDownloadNetworkManager;
        QNetworkReply *GetFileDownloadNetworkReply;
    
        QObject::connect(&GetFileDownloadNetworkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(FileDownloadReplyReceived(QNetworkReply*)));
        QObject::connect(&GetFileDownloadNetworkManager, SIGNAL((QNetworkReply*)), this, SLOT(FileDownloadReplyReceived(QNetworkReply*)));
    
        QUrl url(QString("MyLinkToDownloadFile"));  
        QNetworkRequest reqpn(url);
        reqpn.setRawHeader("Content-Type", "text/xml");
        GetFileDownloadNetworkReply = GetFileDownloadNetworkManager.get(reqpn);
    }
    
    downloadurl::~downloadurl()
    {
    
    }
    
    void downloadurl::FileDownloadReplyReceived(QNetworkReply *Reply)
    {
        if(Reply->error() == QNetworkReply::NoError)
        {
            QFile DownloadedFile("/FlashDisk/ServerTemp/12012.xml");
            if (!DownloadedFile.open(QIODevice::WriteOnly | QIODevice::Text))
            {
                messageBox.critical(0,"Error","Can't open File");
                return;
            }
            DownloadedFile.write(Reply->readAll());
            DownloadedFile.close();
        }
        Reply->deleteLater();
    }