PDA

View Full Version : Convert HTTP QUrl to file path on the server



atomic
5th October 2015, 11:04
Maybe someone know how I can get relative to specific folder file path from url for example

from

"http://doc.qt.io/documents/readme.txt"

get only

documents/readme.txt
without first slash "/"

from

"/video/Qt/example.avi"

get only

video/Qt/example.avi
without first slash "/"

I get several form of url from client applications but I need from that only file path without first "/" because then I can easily doing something like


QFileInfo(path).lastModified();

How I can parse url for this format?

This statement


QFileInfo(url2.path()).filePath();

return file path but always with first slash "/", so then I need to remove it


path.remove(0, 1);

It is a good solution? Or maybe QUrl have some better way?

Thanks,

KarmTepl
5th October 2015, 16:03
QString was not meant for a canonical url representation. It is a string class existing mostly due to the utf use cases.
What you are looking for is QUrl which was meant for use cases like this. Pass your path to that, and then get the "QFile-readable" path out of that, then pass it to QFile.

atomic
5th October 2015, 18:46
This code solved my problem


QString path = QFileInfo(url.path()).filePath();
qDebug()<< QDir("/").relativeFilePath(path);

KarmTepl
5th November 2015, 11:23
QString was not meant for a canonical url representation. It is a string class existing mostly due to the utf use cases. What you are looking for is QUrl which was meant for use cases like this. Pass your path to that, and then get the "QFile-readable" path out of that, then pass it to QFile.