Some way to get the file name with url?
-
Hi i am doing one http client but when i save the file i need to put the same extension so if i download one txt must be txt, php must php etc..
But i can get since the url for example: google.com/file.php <-- this file because i was searching and i checked something that maybe works:QFileInfo fileInfo(url.path()); QString fileName=fileInfo.fileName();
Some way to do this. Thx in advance.
-
Hi @Jeronimo
No need to use
QFileInfo
- just useQUrl::fileName()
directly :)qDebug() << QUrl("http://google.com/file.php").fileName(); // "file.php"
Note, depending on the use case, you might also want to look at the
Content-Disposition
header (if any) included in the HTTP server's response too.Cheers.
-
Hi @Jeronimo
No need to use
QFileInfo
- just useQUrl::fileName()
directly :)qDebug() << QUrl("http://google.com/file.php").fileName(); // "file.php"
Note, depending on the use case, you might also want to look at the
Content-Disposition
header (if any) included in the HTTP server's response too.Cheers.
@Paul-Colby said in Some way to get the file name with url?:
QUrl("http://google.com/file.php").fileName();
Ok thx one question about one issue that i have about this. I call since my main function and when i call the method i pass the string. But i dont know how i can use for example since my main function to say one string (the url of my website) and this will be used in my slot of my other class.
Main cpp:
#include <QCoreApplication> #include "downloader.h" int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); Downloader d; d.doDownload("http://mywebsite/currxml.php"); return a.exec(); }
Downloader.h:
#ifndef DOWNLOADER_H #define DOWNLOADER_H #include <QObject> #include <QNetworkAccessManager> #include <QNetworkRequest> #include <QNetworkReply> #include <QUrl> #include <QDateTime> #include <QFile> #include <QDebug> class Downloader : public QObject { Q_OBJECT public: explicit Downloader(QObject *parent = 0); void doDownload(QString s); signals: public slots: void replyFinished (QNetworkReply *reply); private: QNetworkAccessManager *manager; }; #endif // DOWNLOADER_H
Downloader.cpp:
#include "downloader.h" Downloader::Downloader(QObject *parent) : QObject(parent) { } void Downloader::doDownload(QString s) { manager = new QNetworkAccessManager(this); connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*))); manager->get(QNetworkRequest(QUrl(s))); } void Downloader::replyFinished (QNetworkReply *reply) { if(reply->error()) { qDebug() << "ERROR!"; qDebug() << reply->errorString(); } else { qDebug() << reply->header(QNetworkRequest::ContentTypeHeader).toString(); qDebug() << reply->header(QNetworkRequest::LastModifiedHeader).toDateTime().toString(); qDebug() << reply->header(QNetworkRequest::ContentLengthHeader).toULongLong(); qDebug() << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); qDebug() << reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString(); QString p =QUrl("http://myweb/currxml.php").fileName(); QFile *file = new QFile("C:/Users/moh/"+p); if(file->open(QFile::Append)) { file->write(reply->readAll()); file->flush(); file->close(); } delete file; } reply->deleteLater(); }
If you have some suggestion will help me a lot sorry again!
-
Instead of
QString p =QUrl("http://myweb/currxml.php").fileName();
try
QString p = reply->request().url().fileName();
Cheers.