Downloading data from nseindia
-
Hi Experts ,
I am downloading data from www.nseinda.com
and the link is "http://www.nseindia.com/content/historical/EQUITIES/2015/JAN/cm12JAN2015bhav.csv.zip"But i am downloading 0 size file ..
anything wrong ? or any working program for downloading files from web?
@downloader::downloader(QObject *parent) :
QObject(parent)
{myprocedure();
}
void downloader::downloadFile(const QString &url, const QString &aPathInClient)
{QNetworkAccessManager m_NetworkMngr; QNetworkReply *reply= m_NetworkMngr.get(QNetworkRequest(url)); QEventLoop loop; QObject::connect(reply, SIGNAL(finished()),&loop, SLOT(quit())); loop.exec(); QUrl aUrl(url); QFileInfo fileInfo=aUrl.path(); QFile file(aPathInClient+"\\"+fileInfo.fileName()); file.open(QIODevice::WriteOnly); file.write(reply->readAll()); delete reply;
}
void downloader::myprocedure()
{QString path; QString localpath; QMessageBox msgBox; QMessageBox msgBox2; path = QStandardPaths::writableLocation(QStandardPaths::HomeLocation); path = "C:/Users/ryerajan/build-untitled2-Desktop_Qt_5_3_MinGW_32bit-Debug/base/download/cm01OCT2014bhav.dbf.zip"; localpath = path;
// QMessageBox msgBox2;
msgBox2.setText("You can download the update from the web.");
msgBox2.setInformativeText("Do you want to download the file now?");
msgBox2.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
msgBox2.setDefaultButton(QMessageBox::Yes);
int ret = msgBox2.exec();if (ret == QMessageBox::Yes) { downloadFile ("http://www.nseindia.com/content/historical/EQUITIES/2014/OCT/cm01OCT2014bhav.dbf.zip", localpath); msgBox.setText("Your file should be updated now"); msgBox.exec(); } else { // Do something else }
}@
-
You can use Q_FUNC_INFO instead of FILE<<" : " << FUNCTION . It is less to type :-)
-
Your code has many small issues:
- you are not checking QNetworkReply for errors
- you are not checking if QFile::open() was successful
- you are using \ as the delimiter instead of / (Qt uses Unix paths internally)
- you are passing a full file name to downloadFile() method, but you then append another file name using fileInfo.path(). I seriously doubt that fileInfo is not a valid path, because you try to create it from a QUrl. Please check the path and verify that it is correct
- you are deleting the QNetworkReply object instead of calling deleteLater()
- you do not check whether the QNetworkReply actually has any data
I suggest running this code through a debugger and going step by step to see what is happening.
-
Hi,
It seems that you are not closing the file.
-
[quote author="p3c0" date="1421151089"]Hi,
It seems that you are not closing the file.[/quote]
This is not required when using QFile created on a stack.
NOTE: I've moved your post, p3c0, to this thread because the OP has double-posted.