Reputation: 30895
i need to check the if file exists on http server, i have the full path and when i try it via browser all works but when i try in code to do :
if(QFile::exists("http://www.foo.com/hidden/Support/myapp_1.1.2_installer.exe" ))
{
qDebug("file exists");
return true;
}
else
{
qDebug("file not exists");
}
as it writen here : http://www.qtcentre.org/archive/index.php/t-43712.html?s=b9ae49962c9219aec93b43c514e2ba33
it allways returns me false no matter what .. what im doing wrong and is it the right way to do this ?
Upvotes: 2
Views: 2290
Reputation: 424
The Qt class QFile can only deal with files on local filesystem.
You can try out using Qt Network module, probably like this:
QNetworkAccessManager *nam = new QNetworkAccessManager(this);
....
QNetworkRequest req(QUrl("http://www.foo.com/hidden/Support/myapp_1.1.2_installer.exe"));
QNetworkReply *reply = nam->get(req);
connect(reply, SIGNAL(metaDataChanged()),
this, SLOT(slotMetaDataChanged()));
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),
this, SLOT(slotNetworkError(QNetworkReply::NetworkError)));
NOTE THAT if you only want to check for the file's existence, you DON'T want to connect to the finished(QNetworkReply*) signal, because the signal will only be emitted when the network reply has finished processing. That is, the signal will only be emitted after the file is totally downloaded if the file exists.
Then,
Upvotes: 1
Reputation: 3989
The way you are trying to do this, is totally wrong. QFile isn't able to query a webserver. What you need to do is use the QNetworkAccessManager class. With this you can try to download your myapp_1.1.2_installer.exe. If the file does not exist, you will get an error message.
Why the poster on qtcente.org claims it worked for him... no idea. Maybe because his address was a local one. But it still smells fishy.
QNetworkAccessManager *manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply*)),
this, SLOT(replyFinished(QNetworkReply*)));
manager->head(QNetworkRequest(QUrl("www.foo.com/hidden/Support/myapp_1.1.2_installer.exe")));
The QNetworkReply in the replyFinished slot has the method NetworkError QNetworkReply::error() const
.
You should get a QNetworkReply::ContentNotFoundError if your file does not exist.
Edit: As several comments pointed out, just to learn the existence of a file on a remote server using 'get' and connecting to replyFinished might not be the best of ideas. Might be ok for very small files, but definitely overkill for large blobs of data. I changed the 'get' request into a 'head'. Turner's solution will work, mine should now be an acceptable alternative.
Upvotes: 0
Reputation: 8975
The function QFile::exists
is not able to create HTTP requests, which would be necessary to achieve what you are trying to do. The forum discussion you linked to works, because the guy is trying to access a network drive; this is naturally supported by the operating system.
To check whether the file exists, you will have to go the long way around - here is an explanation of how to communicate with a web server: http://developer.nokia.com/Community/Wiki/Creating_an_HTTP_network_request_in_Qt
Upvotes: 1