[ASK] QFile can't read file after file downloaded from url(from http example)?



  • I made an application, the application requires files from the internet.
    after the file has been downloaded, the file can not be opened by qfile?
    but, qfile can read it (if without doing even DownloadFile)

    Or anyone can help how to read files located on a site?

    Note: I'm working on ubuntu 14.04 LTS
    Here's an example of the application ..

    ews.h
    @
    #ifndef EWS_H
    #define EWS_H

    #include <QMainWindow>
    #include <QUrl>
    #include <QtCore>
    #include <QtNetwork/QNetworkAccessManager>
    class QAuthenticator;
    class QNetworkReply;

    namespace Ui {
    class EWS;
    }

    class EWS : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit EWS(QWidget *parent = 0);
    void startRequest(QUrl url);
    void readFile(QString files);
    ~EWS();

    private slots:

    void httpFinished();
    void httpReadyRead();
    QString downloadFile&#40;QUrl uri&#41;;
    
    
    void on_pushButton_clicked(&#41;;
    

    private:
    bool httpRequestAborted = false;
    int httpGetId;

    QUrl url; QFile *file;
    QNetworkAccessManager qnam; QNetworkReply *reply;
    
    Ui::EWS *ui;
    

    };

    #endif // EWS_H
    @

    ews.cpp
    @
    #include "ews.h"
    #include "ui_ews.h"
    #include <QtCore>
    #include <QtNetwork>
    #include <QMessageBox>

    EWS::EWS(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::EWS)
    {
    ui->setupUi(this);

    // --- START --- //
    QString filePath = NULL;
    
    filePath = downloadFile&#40;QUrl("https://inatews.bmkg.go.id/rss/capatomlast40event.xml"&#41;&#41;;
    // read file atom
    ui->lineEdit->setText(filePath);
    
    if (filePath != NULL)
    {
        readFile&#40;filePath&#41;;
    }
    

    }

    EWS::~EWS()
    {
    delete ui;
    }

    void EWS::httpFinished()
    {
    if (httpRequestAborted) {
    if (file) {
    file->close();
    file->remove();
    delete file;
    file = 0;
    }
    reply->deleteLater();
    return;
    }

    file->flush();
    file->close();
    
    QVariant redirectionTarget = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
    if (reply->error()) {
        file->remove();
    } else if (!redirectionTarget.isNull()) {
        QUrl newUrl = url.resolved(redirectionTarget.toUrl());
    
    } else {
        //QString fileName = QFileInfo(QUrl(urlLineEdit->text()).path()).fileName();
    
    }
    
    reply->deleteLater();
    reply = 0;
    delete file;
    file = 0;
    

    }

    void EWS::httpReadyRead()
    {
    // this slot gets called every time the QNetworkReply has new data.
    // We read all of its new data and write it into the file.
    // That way we use less RAM than when reading it at the finished()
    // signal of the QNetworkReply
    if (file)
    file->write(reply->readAll());
    }

    void EWS::startRequest(QUrl url)
    {
    reply = qnam.get(QNetworkRequest(url));
    connect(reply, SIGNAL(finished()),
    this, SLOT(httpFinished()));
    connect(reply, SIGNAL(readyRead()),
    this, SLOT(httpReadyRead()));
    connect(reply, SIGNAL(downloadProgress(qint64,qint64)),
    this, SLOT(updateDataReadProgress(qint64,qint64)));
    }

    QString EWS::downloadFile(QUrl uri)
    {
    //url = "https://inatews.bmkg.go.id/rss/capatomlast40event.xml";
    url = uri;

    // buat dir temporary u/ menyimpan atom
    if( QDir(QDir::currentPath(&#41; + "/tmp/").exists() == false )
        QDir().mkpath(QDir::currentPath() + "/tmp/");
    
    // buat file di tmp folder
    QFileInfo fileInfo(url.path());
    QString fileName = QDir::currentPath() + "/tmp/" + fileInfo.fileName();
    //QString fileName = fileInfo.fileName();
    if (fileName.isEmpty())
        fileName = "index.html";
    
    // jika file exists, timpa filenya..
    if (QFile::exists(fileName)) {
        QFile::remove(fileName);
    }
    
    file = new QFile&#40;fileName&#41;;
    if (!file->open(QIODevice::WriteOnly&#41;) {
        //QDebug (this, tr("HTTP"),
        //                         tr("Unable to save the file %1: %2.")
        //                         .arg(fileName).arg(file->errorString()));
        delete file;
        file = 0;
        file->close(); QFile&#40;fileName&#41;.close(&#41;;
        return NULL;
    }
    
    // mulai permintaan unduh
    httpRequestAborted = false;
    startRequest(url);
    
    file->flush();file->close();
    QFile&#40;fileName&#41;.flush(&#41;;QFile&#40;fileName&#41;.close(&#41;;
    return fileName;
    

    }

    void EWS::readFile(QString files)
    {
    QFile filex(files);// = new QFile(file);
    //file.setFileName(files);

    filex.open(QIODevice::ReadOnly | QIODevice::Text);
    QTextStream in(&filex);
    while(!in.atEnd())
    {
        QString line = in.readLine();
        ui->plainTextEdit->appendPlainText(files);
        ui->plainTextEdit->appendPlainText(line);
        //qDebug() << line;
        //OR qDebug() << line.toLocal8Bit; None of these ways work
    }
    filex.close();
    

    }
    @


  • Moderators

    Hi,

    Have you made sure that the downloaded file is available to readFile(filePath) function when the control enters in it?
    Since you are downloading a file from the network it may not be available immediately.



  • Hi,
    first of all I would mind you not to do this:
    @
    delete file;
    file = 0;
    file->close();
    @
    You killed and removed file, you can't go around and calling member functions anymore. It's not there anymore!! Also in httpReadread function you just flush/close the file even when no new one created!
    IYAM you should only 'delete' an object in the same scope as where you created it. If you create it in a function, delete it in a function. If you create it in a constructor, delete in a destructor etc.

    Before you open a file, please first check to see if it exists. When the file does not exists you get an empty one when opening.
    That might solve the puzzle?



  • @p3c0
    file exists at the address downloads

    @Jeroentje@home
    where should I put the file-> close (); ?
    because if I change it, the downloaded file becomes corrupted


Log in to reply
 

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