File shown empty when I downlad from url and try to read it
-
I'm trying download txt file from url and read it in the same function.
When I try to read it, shown empty on QDebug.
I'm monitoring the file in folder and in terminal with debug. When I download the file it shows as 0 kb. Then it reads the file as empty and the size of the file becomes 1kb after reading. I guess I need to get some sleep in the part where reading. I tried,QThread::msleep(6000);
but same. How can I solve this issue. Here is my code;
#include "setting.h" #include "ui_setting.h" #include <QNetworkAccessManager> #include <QUrl> #include <QDebug> #include <QFile> #include <QIODevice> #include <QThread> setting::setting(QWidget *parent) : QDialog(parent), ui(new Ui::setting) { ui->setupUi(this); } setting::~setting() { delete ui; } void setting::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 setting::updateDownloadProgress(qint64 bytesRead, qint64 totalBytes) //{ //if (httpRequestAborted) // return; //progressDialog->setMaximum(totalBytes); //progressDialog->setValue(bytesRead); //} // During the download progress, it can be canceled void setting::cancelDownload() { httpRequestAborted = true; reply->abort(); } // When download finished or canceled, this will be called void setting::httpDownloadFinished() { // when canceled if (httpRequestAborted) { if (file) { file->close(); file->remove(); delete file; file = 0; } reply->deleteLater(); progressDialog->hide(); return; } // download finished normally //progressDialog->hide(); file->flush(); file->close(); // get redirection url QVariant redirectionTarget = reply->attribute(QNetworkRequest::RedirectionTargetAttribute); if (reply->error()) { file->remove(); QMessageBox::information(this, tr("HTTP"), tr("Download failed: %1.") .arg(reply->errorString())); } else if (!redirectionTarget.isNull()) { QUrl newUrl = url.resolved(redirectionTarget.toUrl()); if (QMessageBox::question(this, tr("HTTP"), tr("Redirect to %1 ?").arg(newUrl.toString()), QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) { url = newUrl; reply->deleteLater(); file->open(QIODevice::WriteOnly); file->resize(0); startRequest(url); return; } } else { //QString fileName = QFileInfo(QUrl("http://example.com/test.txt").path()).fileName(); } reply->deleteLater(); reply = 0; delete file; file = 0; manager = 0; } // This will be called when download button is clicked void setting::startRequest(QUrl url) { // get() method posts a request // to obtain the contents of the target request // and returns a new QNetworkReply object // opened for reading which emits // the readyRead() signal whenever new data arrives. reply = manager->get(QNetworkRequest(url)); // Whenever more data is received from the network, // this readyRead() signal is emitted connect(reply, SIGNAL(readyRead()), this, SLOT(httpReadyRead())); // Also, downloadProgress() signal is emitted when data is received connect(reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(updateDownloadProgress(qint64,qint64))); // This signal is emitted when the reply has finished processing. // After this signal is emitted, // there will be no more updates to the reply's data or metadata. connect(reply, SIGNAL(finished()), this, SLOT(httpDownloadFinished())); } void setting::on_pushButton_clicked() { manager = new QNetworkAccessManager(this); // get url url = "http://example.com/test.txt"; QFileInfo fileInfo(url.path()); QString fileName = fileInfo.fileName(); if (fileName.isEmpty()) fileName = "test.txt"; if (QFile::exists(fileName)) { if (QMessageBox::question(this, tr("HTTP"), tr("There already exists a file called %1 in " "the current directory. Overwrite?").arg(fileName), QMessageBox::Yes|QMessageBox::No, QMessageBox::No) == QMessageBox::No) return; QFile::remove(fileName); } file = new QFile(fileName); if (!file->open(QIODevice::WriteOnly)) { QMessageBox::information(this, tr("HTTP"), tr("Unable to save the file %1: %2.") .arg(fileName).arg(file->errorString())); delete file; file = 0; return; } // used for progressDialog // This will be set true when canceled from progress dialog httpRequestAborted = false; // progressDialog->setWindowTitle(tr("HTTP")); //progressDialog->setLabelText(tr("Downloading %1.").arg(fileName)); startRequest(url); QThread::msleep(6000); QString appDir = QDir::currentPath() + "/test.txt"; QString version; qint64 size; QFile file(appDir); if (file.open(QIODevice::ReadOnly | QIODevice::Text)) { QTextStream in ( & file); size = file.size(); version = in .readLine(); file.close(); qDebug() << version; qDebug() << size; }else{ qDebug() << "error "; } }
-
I'm trying download txt file from url and read it in the same function.
When I try to read it, shown empty on QDebug.
I'm monitoring the file in folder and in terminal with debug. When I download the file it shows as 0 kb. Then it reads the file as empty and the size of the file becomes 1kb after reading. I guess I need to get some sleep in the part where reading. I tried,QThread::msleep(6000);
but same. How can I solve this issue. Here is my code;
#include "setting.h" #include "ui_setting.h" #include <QNetworkAccessManager> #include <QUrl> #include <QDebug> #include <QFile> #include <QIODevice> #include <QThread> setting::setting(QWidget *parent) : QDialog(parent), ui(new Ui::setting) { ui->setupUi(this); } setting::~setting() { delete ui; } void setting::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 setting::updateDownloadProgress(qint64 bytesRead, qint64 totalBytes) //{ //if (httpRequestAborted) // return; //progressDialog->setMaximum(totalBytes); //progressDialog->setValue(bytesRead); //} // During the download progress, it can be canceled void setting::cancelDownload() { httpRequestAborted = true; reply->abort(); } // When download finished or canceled, this will be called void setting::httpDownloadFinished() { // when canceled if (httpRequestAborted) { if (file) { file->close(); file->remove(); delete file; file = 0; } reply->deleteLater(); progressDialog->hide(); return; } // download finished normally //progressDialog->hide(); file->flush(); file->close(); // get redirection url QVariant redirectionTarget = reply->attribute(QNetworkRequest::RedirectionTargetAttribute); if (reply->error()) { file->remove(); QMessageBox::information(this, tr("HTTP"), tr("Download failed: %1.") .arg(reply->errorString())); } else if (!redirectionTarget.isNull()) { QUrl newUrl = url.resolved(redirectionTarget.toUrl()); if (QMessageBox::question(this, tr("HTTP"), tr("Redirect to %1 ?").arg(newUrl.toString()), QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) { url = newUrl; reply->deleteLater(); file->open(QIODevice::WriteOnly); file->resize(0); startRequest(url); return; } } else { //QString fileName = QFileInfo(QUrl("http://example.com/test.txt").path()).fileName(); } reply->deleteLater(); reply = 0; delete file; file = 0; manager = 0; } // This will be called when download button is clicked void setting::startRequest(QUrl url) { // get() method posts a request // to obtain the contents of the target request // and returns a new QNetworkReply object // opened for reading which emits // the readyRead() signal whenever new data arrives. reply = manager->get(QNetworkRequest(url)); // Whenever more data is received from the network, // this readyRead() signal is emitted connect(reply, SIGNAL(readyRead()), this, SLOT(httpReadyRead())); // Also, downloadProgress() signal is emitted when data is received connect(reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(updateDownloadProgress(qint64,qint64))); // This signal is emitted when the reply has finished processing. // After this signal is emitted, // there will be no more updates to the reply's data or metadata. connect(reply, SIGNAL(finished()), this, SLOT(httpDownloadFinished())); } void setting::on_pushButton_clicked() { manager = new QNetworkAccessManager(this); // get url url = "http://example.com/test.txt"; QFileInfo fileInfo(url.path()); QString fileName = fileInfo.fileName(); if (fileName.isEmpty()) fileName = "test.txt"; if (QFile::exists(fileName)) { if (QMessageBox::question(this, tr("HTTP"), tr("There already exists a file called %1 in " "the current directory. Overwrite?").arg(fileName), QMessageBox::Yes|QMessageBox::No, QMessageBox::No) == QMessageBox::No) return; QFile::remove(fileName); } file = new QFile(fileName); if (!file->open(QIODevice::WriteOnly)) { QMessageBox::information(this, tr("HTTP"), tr("Unable to save the file %1: %2.") .arg(fileName).arg(file->errorString())); delete file; file = 0; return; } // used for progressDialog // This will be set true when canceled from progress dialog httpRequestAborted = false; // progressDialog->setWindowTitle(tr("HTTP")); //progressDialog->setLabelText(tr("Downloading %1.").arg(fileName)); startRequest(url); QThread::msleep(6000); QString appDir = QDir::currentPath() + "/test.txt"; QString version; qint64 size; QFile file(appDir); if (file.open(QIODevice::ReadOnly | QIODevice::Text)) { QTextStream in ( & file); size = file.size(); version = in .readLine(); file.close(); qDebug() << version; qDebug() << size; }else{ qDebug() << "error "; } }
@mangekoyu So, what did you so far to find out what the problem is?
Is httpReadyRead() called? -
@jsulm I did something like this to fix the problem. I removed the download part of the text file from the push button function and added it to a separate function. I wanted to use push button function only to read txt file. It is currently working flawlessly. When the application is opened, it will download the txt file, then it will read when the button is pressed.